MyBB Community Forums

Full Version: Prevent duplicate SQL queries (fetch_array) for same users posts?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do I make the results visible in the template?

I tried "{$post['facebook_id']}" and "$post['yahoo_id']" in the Postbit template, but nothing shows.

$post['facebook_id'] = $post['yahoo_id'] = '';

if(THIS_SCRIPT == 'showthread.php' && (string)$mybb->input['mode']  != 'threaded') // mybb 1.6
{
    static $my_custom_cache = null;

    if($my_custom_cache === null)
    {
        global $pids;

        $query = $db->query("
                SELECT s.uid, s.facebook, s.yahoo
		FROM ".TABLE_PREFIX."socialusers s
		LEFT JOIN ".TABLE_PREFIX."posts p ON (p.uid=s.uid)
		WHERE p.{$pids}
        ");

        while($social = $db->fetch_array($query))
        {
            $my_custom_cache[$row['uid']] = $row;
			print_r($social); //View the arrays 
        }
    }
}
else
{
    // do a normal query because there is only one post in the page anyways
    // code here should work for postbit_pm, postbit_annoucmenet hooks.
    // postbit_prev (hook while editing a post that may not yet exists) may need special handle
    // Things get complicated the more you dig into details :P
}

if(isset($my_custom_cache[$post['uid']]))
{
    $post['facebook_id'] = htmlspecialchars_uni($my_custom_cache[$post['uid']]['facebook']);
    $post['yahoo_id'] = htmlspecialchars_uni($my_custom_cache[$post['uid']]['yahoo']);
}
else
{
    // this post has no user/social data? Hmm..
} 
First of all:
$my_custom_cache[$row['uid']] = $row;

Needs to be:
$my_custom_cache[$social['uid']] = $social;

And yes, {$post['facebook_id']} and {$post['yahoo_id']} should work as long as you are passing the $post by reference.

Try to use print_r over them to see if they are filled. Also note that those will only be available in the postbit/_classic templates.
It works. It shows 1 extra query, instead of 10 Smile

Thank you very much Omar G. You solved it for me.
It was nada. Toungue Glad it is working.
I have another problem:

The SQL Table looks like this:

UID | FACEBOOK                 | YAHOO         
-------------------------------------------------------
1   | facebook.com/gaming_page | [email protected]
1   | facebook.com/rpg_page    | [email protected]


I need to show 2 separate rows based on:

1. The words 'gaming' and 'rpg' found in the rows.
2. The category fid's (1. general gaming, 2. role playing games).

I know the code to use, but it just isn't working.

if (preg_grep('/gaming/', $social) && in_array($forum['fid'], array(1))) {
    //show the facebook & yahoo rpg_page and email in the admin's postbit of Category 1.
} elseif (preg_grep('/rpg/', $social) && in_array($forum['fid'], array(2))) {
    //show the facebook & yahoo rpg_page and email in the admin's postbit of Category 2.
}

How can I merge this code, with the code before to make it work with multiple UID's in the SQL table?

So my code now is:


$post['facebook_id'] = $post['yahoo_id'] = '';

if(THIS_SCRIPT == 'showthread.php' && (string)$mybb->input['mode']  != 'threaded') // mybb 1.6
{
    static $my_custom_cache = null;

    if($my_custom_cache === null)
    {
        global $pids;

        $query = $db->query("
            SELECT s.uid, s.facebook, s.yahoo
            FROM ".TABLE_PREFIX."socialusers s
            LEFT JOIN ".TABLE_PREFIX."posts p ON (p.uid=s.uid)
            WHERE p.{$pids}
        ");

        while($social = $db->fetch_array($query))
        {
            $my_custom_cache[$social['uid']] = $social;
            print_r($social); //View the arrays 
        }
    }
}
else

if(isset($my_custom_cache[$post['uid']]))
{
	if (preg_grep('/gaming/', $social) && in_array($forum['fid'], array(1))) 
	{
		$post['facebook_id'] = htmlspecialchars_uni($my_custom_cache[$post['uid']]['facebook']);
		$post['yahoo_id'] = htmlspecialchars_uni($my_custom_cache[$post['uid']]['yahoo']);
	    //show the facebook & yahoo gaming_page and email in the admin's postbit of Category 1.
	} 
	elseif (preg_grep('/rpg/', $social) && in_array($forum['fid'], array(2))) 
	{
		$post['facebook_id'] = htmlspecialchars_uni($my_custom_cache[$post['uid']]['facebook']);
		$post['yahoo_id'] = htmlspecialchars_uni($my_custom_cache[$post['uid']]['yahoo']);
	    //show the facebook & yahoo rpg_page and email in the admin's postbit of Category 2.
	}
}
$db->simple_select('users', 'uid', "username LIKE '%{$db->escape_string_like('omar g.')}%'")

UID=25096 That is me here Big Grin

http://dev.mysql.com/doc/refman/5.0/en/p...ching.html
Would that be a viable solution, considering there will be a few other people with 2 separate facebook fan pages, based on different gaming categories?

I won't be able to have multiple sql queries when other users have 2 different facebook fan pages, that will need to be displayed on different categories in the forum.
I would add an extra column named "type" and upon the insertion of new data feeds, that column would be filled with either 'gaming' or 'rpg' and a simple "equals to" check would be enough to determine which row should be used and where. This looks simpler to me. Also, consider using booleans values if you are willing to choose between 2 kind of data.
I would need to see your code to understand better but what Shade suggest seems like the best approach. Add a new field to your DB table to identify the social field category and show them depending on this.
Pages: 1 2