MyBB Community Forums

Full Version: An odd problem with user threads
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I have been working on a Dashboard for my site.
I want it to list the threads that the user is subscribed to, but has new posts.

The odd thing is, it works for me, somewhat. But signed in as another user, its terribly messed up.

Here is what it looks like from my perspective...

When I have no new posts in threads I participated in: http://screencast.com/t/4pPmhJCQ

When people have posted in threads that I did: http://screencast.com/t/tU39Xii5

As you can see, the feature looks perfectly fine. But, it only works for one thread (tid=1) and one user (uid=1)

I log into my test account, I see this: http://screencast.com/t/FWbxXRcMC0FI

There is two pieces of code for this, one to determine the number by the dashboard link, "dashboard (#)" and the other is to list the threads.

Dashboard link code:
<?php
$get_threads = $db->query("SELECT * FROM CENSORED_threadsubscriptions WHERE uid='".$mybb->user['uid']."'") or die($db->error());
$s = $db->fetch_array($get_threads);
$get_info_from = $db->query("SELECT * FROM CENSORED_threads WHERE tid='".$s['tid']."'") or die($db->error());
$thread_c = $db->fetch_array($get_info_from);
if($thread_c['lastposteruid'] != $mybb->user['uid']) 
{
	$dashthreads = 1;
}

$num_dash = count($dashthreads);
?>

And here is the code to list the threads.
<?php
$get_threads = $db->query("SELECT * FROM CENSORED_threadsubscriptions WHERE uid='".$mybb->user['uid']."' ORDER BY sid DESC") or die($db->error());
while($s = $db->fetch_array($get_threads))
{
	$get_info_from = $db->query("SELECT * FROM CENSORED_threads WHERE tid='".$s['tid']."'") or die($db->error());
	$thread = $db->fetch_array($get_info_from);
	if($thread['lastposteruid'] != $mybb->user['uid']) 
	{
		$threads = '<div style="display: block;"><a href="./showthread.php?tid='.$thread['tid'].'"><img src="./images/star.gif" border="0" /> <strong>'.$thread['subject'].'</strong></a> <span style="color: #666;"><em>has new comments.</em></span></div>';
	}
	if(count($threads) == 0) {
		$threads = 'No threads you have posted in have been replied to.';
	}
}
?>

If anyone can help, it would be much appreciated Smile
Before running the second set of code put
unset($s)
or try using a different variable name instead of $s in the second bit of code
(2009-09-04, 07:17 AM)- G33K - Wrote: [ -> ]Before running the second set of code put
unset($s)
or try using a different variable name instead of $s in the second bit of code

Changed, but not the problem.
Upon looking at the code more in depth, I realize thats not the only problem with the code.

Heres a few more, you might have to re-think the way you have it all setup.
-First set of code the second line you're fetching an array which will give you a multi-dimension array but in the next line you're calling a single dimension array($s['tid']), if more than one result is returned in the first query then the second query gets screwed up. Maybe use a while here too?
- Your use of count is incorrect, count works best on arrays, you're using them on variables which will give incorrect results
- $num_dash will be either 0 or 1 because of improper count use, check that piece of code/logic again.
Heres a much simpler code than above and will work for what you want to do here. I've reduced it to a single loop and gathering the queries once only thus reducing the calls to db. Try it out and let me know

$get_threads = $db->simple_select($prefix.'threadsubscriptions', '*', "uid = '".$mybb->user['uid']."'", array('order_by' => 'sid', 'order_dir' => 'DESC'));
$num_dash = 0;
$threads = '';
while($s = $db->fetch_array($get_threads))
{
	$get_info_from = $db->simple_select($prefix.'threads', '*', "tid = '".$s['tid']."'", array('limit' => 1));
	$thread_c = $db->fetch_array($get_info_from);
	if($thread_c['lastposteruid'] != $mybb->user['uid']) 
	{
	    $num_dash++;
	    $threads .= '<div style="display: block;"><a href="./showthread.php?tid='.$thread_c['tid'].'"><img src="./images/star.gif" border="0" /> <strong>'.$thread_c['subject'].'</strong></a> <span style="color: #666;"><em>has new comments.</em></span></div>';
	}
}
if ($num_dash == 0)
{
	$threads = 'No threads you have posted in have been replied to.';
}

$num_dash will contain the number of threads and $threads will contain the list of threads with new posts.
Nope, still only shows 1 thread. I tried getting rid of array('limit' => 1) and nothing.
Can you post your entire code here or in PM because I just tested the code I posted above and it works correctly for me, it shows me the correct number of threads even more than 1 so it seems its getting messed up somewhere else in your code.