I Am Java Posted June 14, 2010 Share Posted June 14, 2010 http://ddf.pastebin.com/CD5XQrYV How can I make that a bit more efficient? Right now I use a while statement to add up $m, which gives a count of how many unread topics there are, and if there are more than 1, then it displays a link. If not, it displays nothing. Right now, since there are 15 forums, the script takes well over 30 seconds to run. How can I count up the amount of unread topics faster? Thanks. Link to comment https://forums.phpfreaks.com/topic/204691-more-efficient-code/ Share on other sites More sharing options...
jonsjava Posted June 14, 2010 Share Posted June 14, 2010 I'm not certain how your database is configured, but wouldn't it be easier to do all the checking on the database? SELECT COUNT(*) AS `total` FROM `forum_thread` WHERE `thread_boardid`='$boardid' AND `read` LIKE '%$logged_id%' Here's the modified version: <?php while($board = mysql_fetch_array($r2)){ $r3 = mysql_query("SELECT * FROM `forum_thread` WHERE `thread_boardid` = '".$board['board_id']."' AND `thread_deleted` = '0' ORDER BY `thread_id` DESC") or die(mysql_error()); $thread_count = mysql_num_rows($r3); $title_get3 = stripslashes($board['board_name']); $title3 = str_replace($title_current,$title_replace,$title_get3); echo " <tr> <td style='text-align:center;'> "; if($logged['userlevel']=='9'){ $boardid = $board['board_id']; $logged_id = $logged['id']; $get_new2 = mysql_query("SELECT COUNT(*) AS `total` FROM `forum_thread` WHERE `thread_boardid`='$boardid' AND `read` LIKE '%$logged_id'") or die(mysql_error()); $m = 1; $row = mysql_fetch_assoc($get_new2); $total = $row['total']; } echo $total; if($total != '0' && $logged['userlevel'] == '200'){ echo " <a href='".$url."forums".$a_act."markread".$a_view.$board['board_id'].$end."'>NEW</a> "; } else { echo " "; } ?> Remember: this is a concept code, so you will need to change it to fit your needs, but if I understand what you are needing, that should be the way you want to go: 1) find all threads that are unread for that user 2) if there is anything unread, show them a link to mark them read. Link to comment https://forums.phpfreaks.com/topic/204691-more-efficient-code/#findComment-1071895 Share on other sites More sharing options...
ignace Posted June 14, 2010 Share Posted June 14, 2010 1. while($board=mysql_fetch_array($r2)){ $r3=mysql_query("SELECT * FROM `forum_thread` WHERE `thread_boardid` = '".$board['board_id']."' AND `thread_deleted` = '0' ORDER BY `thread_id` DESC") or die(mysql_error()); This is the equivalent of a JOIN, use it: SELECT * FROM forum_thread JOIN forum_board ON forum_thread.thread_boardid = forum_board.board_id ORDER BY thread_id DESC Link to comment https://forums.phpfreaks.com/topic/204691-more-efficient-code/#findComment-1071918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.