reduce queries?
#1
I have a table that stores each thread tags not I wanna in the forums display threads list I am fetching each thread tags and show them there but it my code generates 1 query for each thread please help me to reduce it!


$plugins->add_hook("forumdisplay_thread","test");
function test()
{
	global $db, $forumtest, $thread;

	$query = $db->simple_select("tags", "tags", "tid=".$thread['tid']);
	$forumtest = $db->fetch_array($query);

	if (!empty($forumtest['tags']))
	{
    $x = preg_replace('/\s*,\s*/', ', ', $forumtest['tags']);
    $forumtest = $x;
	}
	else
	{
		$forumtest = "";
	}
}
Reply
#2
You can use the same hook and query the data related to all threads being currently displayed when your function is executed for the first time (the $tids variable from forumdisplay.php contains thread IDs), and save it in an array, so it can be accessed for each thread without subsequent queries.
devilshakerz.com/pgp (DF3A 34D9 A627 42E5 BC6A 6750 1F2F B8AA 28FF E1BC) ▪ keybase.io/devilshakerz
Reply
#3
Would you mind to post some example code cause for me it's like trying to build a space shuttle Smile
Reply
#4
This one relates:
http://mybbhacks.zingaburga.com/forumdisplay.php?fid=32

But you need to put in some effort..
Reply
#5
I am putting into it more than enough effort but since I have only have ideas I can't make it work please show me some example!

I would do like this!

On forumdiplay start have a query that will fetch all thread tags based on the forum $tids make a global value then hook to each thread and use that global value so grab tags based on the each thread tid.
Reply
#6
(2015-03-08, 01:49 PM)marcus123 Wrote: On forumdiplay start have a query that will fetch all thread tags based on the forum $tids make a global value then hook to each thread and use that global value so grab tags based on the each thread tid.

In this case you can't query data in forumdisplay_start because at this point $tids is not yet available (you can find where your hooks would be executed by searching for $plugins->run_hooks in forumdisplay.php).

In this example you would need to put $tids and $tags_cache into the global declaration.
// get tags for threads on first run
if (!isset($tags_cache)) {
    $tags_cache = array();
    $query = $db->simple_select("tags", "tags", "tid IN (".$tids.")");
    while ($tags = $db->fetch_array($query)) {
        $tags_cache[ $tags['tid'] ] = $tags;
    }
}

// get thread tags from cache
if (isset($tags_cache[ $thread['tid'] ])) {
    $thread_tags = $tags_cache[ $thread['tid'] ];
} else {
    $thread_tags = false;
}
devilshakerz.com/pgp (DF3A 34D9 A627 42E5 BC6A 6750 1F2F B8AA 28FF E1BC) ▪ keybase.io/devilshakerz
Reply
#7
Buddy thanks so much!

function tags_forumdisplay_thread()
{
	global $db, $tids;
	
	// get tags for threads on first run
if (!isset($tags_cache)) {
    $tags_cache = array();
    $query = $db->simple_select("tags", "*", "tid IN (".$tids.")");
    while ($tags = $db->fetch_array($query)) {
        $tags_cache[ $tag['tid'] ] = $tags;
    }
}

// get thread tags from cache
if (isset($tags_cache[ $thread['tid'] ])) {
    $thread_tags = $tags_cache[ $thread['tid'] ];
} else {
    $thread_tags = false;
}	
}




I am hooked to forumdisplay_thread and it seems to generate the same amount of queries! Also get whole munch of Array
Do I need another hook like end!
Reply
#8
Because you need to make the variable static at the beginning of the function.
static $tags_cache = array();
Reply
#9
Guys thanks very much but I am so dumb or what! Now it's not showing anything!


function tags_forumdisplay_thread()

{
	static $tags_cache = array(); 
    global $db, $tids, $thread;
     
    // get tags for threads on first run
if (!isset($tags_cache)) {
    $tags_cache = array();
    $query = $db->simple_select("tags", "tags", "tid IN (".$tids.")");
    while ($tags = $db->fetch_array($query)) {
        $tags_cache[ $tag['tid'] ] = $tags;
    }
}
// get thread tags from cache
if (isset($tags_cache[ $thread['tid'] ])) {
    $thread_tags = $tags_cache[ $thread['tid'] ];
} else {
    $thread_tags = false;
} 

  
} 


Should I use
$thread_tags
in the template or what?

Is this only to cache the tags or I will have use it with some hook?

There is something wrong with this part!

if (isset($tags_cache[$thread['tid']])) {
    $thread_tags = $tags_cache[$thread['tid']];
} else {
    $thread_tags = false;
} 

I seem to get it working but now how do I get those tags from that array and add them to correct threads?


function tags_forumdisplay_thread()

{ 
   static $tags_cache = array();
    global $db, $tids, $tags_cache;
   
    // get tags for threads on first run
if (!isset($tags_cache)) {
    $tags_cache = array();
    $query = $db->simple_select("tags", "tags, tid", "tid IN (".$tids.")");
    while ($tags = $db->fetch_array($query)) {
        $tags_cache[$tag['tid']] = $tags;
		
		
   
print_r($tags_cache[$thread['tid']]);
// get thread tags from cache
if (isset($tags_cache[$thread['tid']])) {
    $thread_tags = $tags_cache[$thread['tid']];
} else {
    $thread_tags = false;
} 
}
}


}
Reply
#10
$thread is not globalised, so $thread_tags is always false..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)