Clarkeez Posted August 4, 2011 Share Posted August 4, 2011 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] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 4, 2011 Share Posted August 4, 2011 Posting code as an attachment is not the best way to get help; most people won't download some random file. Post the code within . . . BBCode tags instead. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 4, 2011 Share Posted August 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
Clarkeez Posted August 5, 2011 Author Share Posted August 5, 2011 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 ? Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 5, 2011 Share Posted August 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
Clarkeez Posted August 5, 2011 Author Share Posted August 5, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.