PDA

View Full Version : Any way to optimize my code or cut down on queries?


zeroality
07-31-2006, 04:16 AM
I took this MOD:
https://vborg.vbsupport.ru/showthread.php?t=97403

And turned it into this:
51318

I did it without any real prior knowledge of php or mySQL so I'm sure my code is very inefficient. I pieced it together from what was already in the MOD and got the rest from trial & error.

Here's the plugin code. Any feedback is appreciated, thank you.

Original Code:
// ### LATEST NEWS BY TANTAWI ############################################
$latesta = $db->query_first('SELECT title,threadid,views,replycount FROM ' . TABLE_PREFIX . 'thread WHERE 1 AND forumid = 28 ORDER BY dateline DESC LIMIT 1');
$lns_show = "<a href=\"showthread.php?" . $vbulletin->session->vars['sessionurl'] . "t=$latesta[threadid]\">$latesta[title]</a>";

My Code:
// ### LATEST NEWS BY TANTAWI ############################################
$latesta = $db->query_first('SELECT title,threadid,views,replycount,postusername,datel ine FROM ' . TABLE_PREFIX . 'thread WHERE 1 AND forumid = 28 ORDER BY dateline DESC LIMIT 0 , 1');
$lns_show = "<a href=\"showthread.php?" . $vbulletin->session->vars['sessionurl'] . "t=$latesta[threadid]\">$latesta[title]</a>";
$lnsurl_show = "<a href=\"showthread.php?" . $vbulletin->session->vars['sessionurl'] . "t=$latesta[threadid]\">[Read More]</a>";
$lns2_show = "<b>Replies:</b> $latesta[replycount] <b>Views:</b> $latesta[views]";
$lns3_show = "<a href=\"http://pokerealm.net/forums/member.php?do=getinfo&username=$latesta[postusername]\">$latesta[postusername]</a>";

$latgetpost = $db->query_first('SELECT pagetext FROM ' . TABLE_PREFIX . 'post WHERE 1 AND threadid = ' . $latesta[threadid]. ' ORDER BY dateline ASC LIMIT 0 , 1');
$lat_show = "$latgetpost[pagetext]";
$postchars = 250;
$cuttedpost = substr($latgetpost[pagetext],$postchars,1);
if($cuttedpost !=" "){
while($cuttedpost !=" "){
$i=1;
$postchars=$postchars+$i;
$cuttedpost = substr($latgetpost[pagetext],$postchars,1);
}
}
$cuttedpost = substr($latgetpost[pagetext],0,$postchars);

$authshow = vbdate($vbulletin->options['dateformat'], $latesta['dateline']);
$authshow2 = vbdate($vbulletin->options['timeformat'], $latesta['dateline']);

$amtans = $db->query_first('SELECT threadcount FROM ' . TABLE_PREFIX . 'forum WHERE 1 AND forumid = 28 ORDER BY forumid DESC LIMIT 0 , 1');
$amt_show = "$amtans[threadcount]";

zeroality
07-31-2006, 08:57 PM
Bump.

Paul M
07-31-2006, 09:06 PM
In your queries, you can remove the bold bits as per the example below;

SELECT threadcount FROM ' . TABLE_PREFIX . 'forum WHERE 1 AND forumid = 28 ORDER BY forumid DESC LIMIT 0 , 1

'WHERE 1' is totally pointless, and the limit is unnecessary as you are using query_first.