Jump to content

More Efficient Code


I Am Java

Recommended Posts

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
Share on other sites

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
Share on other sites

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
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.