Jump to content

Optimization


Clarkeez

Recommended Posts

Hey all, I've just looked over my code and I'm wondering if any of you guru's can look over it and see if there is anyway I can improve the speed or performance of this script at all.

I'm a little worried the way I have a while loop, then within that, another while loop with more queries inside it, but I can't see any other way to do things.

I look at code of software like bbpress and phpbb and it all seems very tight compared to mine.

Any advice on my coding will be nice :)

Thanks

 

p.s. file is attached, this script shows a list of forums from a DB

 

[attachment deleted by admin]

Link to comment
Share on other sites

I'm a little worried the way I have a while loop, then within that, another while loop with more queries inside it, but I can't see any other way to do things.

 

Never, ever run queries in a loop. 95% of the time there is a way to do the same thing with a single query. The other 5% of the time you are doing something you shouldn't be doing.

Link to comment
Share on other sites

Hi thanks for the reply.

 

The reason I put queries in a while loop is because I need the data from the first query to use in the next query.

 

I've never written queries like that, I've just done simple SELECT, DELETE, SET etc.

 

I'm not sure how I could get the data from part of a query then use it in the next part :S ?

Link to comment
Share on other sites

Hi

 

Using a Mac at the moment and no real way to test this so almost certainly a load of typos.

 

However something like this should work. Hopefully you can get the idea.

 

<?php
# Load Title
echo '<div class="title" id="t_forum">Forums</div>';
# Get Forum Names & Info
$q_forums = query("SELECT a.id AS aid, a.title AS atitle, b.id AS bid, b.title AS btitle, b.descr, COUNT(c.id)  AS topicCount
				FROM forum a
				LEFT OUTER JOIN forum b ON a.id = b.parent AND a.parent = '0' and a.sub = '0' AND b.sub = '1'
				LEFT OUTER JOIN topic c ON b.id = c.id  AND c.blog = '0' AND c.del = '0'
				ORDER BY a.ord, b.ord");
# Display
echo '<table cellpadding="0" cellspacing="0" border="0"
		<tr><th>Forums</th><th class="center">Topics</th></tr>';
# Get Forum:Title Data
$PrevAid = 0;
while($d_forums = mysql_fetch_array($q_forums)) 
{
	if ($PrevAid != $d_forums['aid'])
	{
		if ($PrevAid != 0)
		{
			echo '<tr><td></td><td></td></tr><tr><td></td><td></td></tr>'; // Spacing between categories
		}
		echo '<tr><td class="forum_titledescr"><span class="forum_title" style="font-size:16pt;">'.$d_forums['atitle'].'</span></td><td class="forum_topiccount"></td></tr>';
		$PrevAid = $d_forums['aid'];
	}
	# Get Forum:Forum Data
	echo '<tr class="row">
			<td class="forum_titledescr"><a href="forum.php?id='.$d_forums['bid'].'"><span class="forum_title" id="spacer">'.$d_forums['btitle'].'</span><span class="forum_descr"> - '.$d_forums['descr'].'</span></a></td>
			<td class="forum_topiccount">'.$d_forums['topicCount'].'</td>
		</tr>';
	if ($PrevAid != $d_forums['aid'])
	{
		echo '<tr><td></td><td></td></tr><tr><td></td><td></td></tr>'; // Spacing between categories
		$PrevAid = $d_forums['aid'];
	}
}
if ($PrevAid != 0)
{
	echo '<tr><td></td><td></td></tr><tr><td></td><td></td></tr>'; // Spacing between categories
}
echo '</table>';
# Load Footer
require('inc/frag/footer.php');
   
?>

 

All the best

 

Keith

Link to comment
Share on other sites

Keith, thank you very much man its nice to see how that works in my query so I can relate to it.

 

I'm going to study that tomorrow and experiement with different things so I can really get used to it, and then hopefully replace all my projects with queries in while loops with this method.

 

Thanks again, this forum has some really helpful people!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.