Skip to content

Instantly share code, notes, and snippets.

@leefish
Created October 23, 2012 09:13
Show Gist options
  • Save leefish/3937792 to your computer and use it in GitHub Desktop.
Save leefish/3937792 to your computer and use it in GitHub Desktop.
A simple demonstration of how to cut the number of queries used in a MyBB plugin using the postbit hook
<?php
function myxbl_postbit(&$post) {
global $mybb, $db, $lang, $pids;
$lang->load('myxbl');
static $myxbl;
if (!is_array($myxbl) && $mybb->input['method'] != "quickreply") // Check that the $myxbl variable is not an array. Also ensure we are not using the quick reply - without this check quick replies won't bne posted right as the $pids variable cannot be referenced
{
$myxbl = array(); // make sure the $myxbl variable is definitely of type array
$xblQuery = $db->write_query("SELECT p.uid, x.valid, x.gamertag, x.game1, x.game2, x.game3 FROM ".TABLE_PREFIX."posts p INNER JOIN ".TABLE_PREFIX."xboxlive x ON (p.uid = x.uid) WHERE ".$pids); // run the query - not we use the ::write_query() method , ::query() is outdated
while ($row = $db->fetch_array($xblQuery)) // run a loop as we fetch the results of the array
{
$myxbl[$row['uid']] = $row; // populate the $myxbl array
}
}
elseif ($mybb->input['method'] == "quickreply") // we're using the quickreply feature so we'll have to do a standard query using the post's UID - we could use the PID but why would we in this case?
{
$xblQuery = $db->simple_select('xboxlive', '*', 'uid = \''.$post['uid'].'\''); // run the query
$myxbl[$post['uid']] = $db->fetch_array($xblQuery); // insert the result into the array
}
// rest of function...
// we can now use the following variable in our postbit and potbit_classic templates to display the information relevant to the postbit: {$post['{$post['uid']}']}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment