MyBB Community Forums

Full Version: Custom Page, Query/While Issues
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I cannot quite put my finger on what the issue may be here but I've got multiple things going on that have completely stumped me.

A lot of the code I have I have copied and pasted from either forumdisplay.php, search.php or showthread.php in hopes of being able to use the same functionality for my specific needs.

However, last post date and last post user are NOT wanting to cooperate AT ALL.

As it stands right now this code is is putting the most recent last poster and that last posters date/time as the last post for several (but not all) of these threads that pop up. So I have obviously done something wrong. HOWEVER, the ORDER BY clause for ordering by lastpost DESC is correct so extra weird (see screenshot). Also it indicates some of the last posters (that is not the multiple repeating one) as a last poster that is not even in said thread. So last post user is completely inaccurate.

Any and all help would be greatly appreciated!!

THE CODE:
<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'lonelythreads.php');
$templatelist = "lonelythreads,lonelythreads_entry,lonelythreads_empty";
require "./global.php";
add_breadcrumb("Lonely Threads", "lonelythreads.php");

$query = $db->query("
    SELECT *
    FROM ".TABLE_PREFIX."threads t
    LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=t.uid)
	LEFT JOIN ".TABLE_PREFIX."forums f ON (t.fid=f.fid)
	WHERE t.prefix IN (1) AND t.visible > 0 AND t.replies <= 2 AND t.lastpost >= ".TIME_NOW." - (30 * 86400)
	ORDER BY t.lastpost DESC
");

while($thread=$db->fetch_array($query))
{
    $bgcolor = alt_trow();
    
    // thread link
    $thread['threadlink'] = get_thread_link($thread['tid']);
	
	// prefix
	$thread['threadprefix'] = $threadprefix = '';
		if($thread['prefix'] != 0)
		{
			$threadprefix = build_prefixes($thread['prefix']);
			if(!empty($threadprefix))
			{
				$thread['threadprefix'] = $threadprefix['displaystyle'].'&nbsp;';
			}
		}
		
	// author
	$thread['author'] = $thread['uid'];
		if(!$thread['username'])
		{
			if(!$thread['threadusername'])
			{
				$thread['username'] = $thread['profilelink'] = htmlspecialchars_uni($lang->guest);
			}
			else
			{
				$thread['username'] = $thread['profilelink'] = format_name($thread['username'], $thread['usergroup'], $thread['displaygroup']);
			}
		}
		else
		{
			$thread['username'] = format_name($thread['username'], $thread['usergroup'], $thread['displaygroup']);
			$thread['profilelink'] = build_profile_link($thread['username'], $thread['uid']);
		}
	
	// thread last poster link NOT WORKING?
	$lastposteruid = $thread['lastposteruid'];
		if(!$lastposteruid && !$thread['lastposter'])
		{
			$lastposter = htmlspecialchars_uni($lang->guest);
		}
		else
		{
			$lastposter = htmlspecialchars_uni($thread['lastposter']);
		}
		$lastpostdate = my_date('relative', $thread['lastpost']);

		// Don't link to guest's profiles (they have no profile).
		if($lastposteruid == 0)
		{
			$lastposterlink = $lastposter;
		}
		else
		{
			$lastposterlink = build_profile_link($lastposter, $lastposteruid);
		}
    
    // thread last post link
	$thread['lastpostlink'] = get_thread_link($thread['tid'], 0, "lastpost");
	
	// thread last post date NOT WORKING?
	$thread['lastpost'] = my_date('relative', $thread['lastpost']);
	
	// forum
	$thread['forumlink_link'] = get_forum_link($thread['fid']);
	$thread['forumlink_name'] = $thread['name'];
	
	// icon IGNORE THIS, UNFINISHED
    $icon['path'] = str_replace("{theme}", $theme['imgdir'], $icon['path']);
	$icon['path'] = htmlspecialchars_uni($icon['path']);
	$icon['name'] = htmlspecialchars_uni($icon['name']);
	
	// folder IGNORE THIS, UNFINISHED
	$folder = '';
	$folder_label = '';
	
	eval("\$lonelythreads .= \"".$templates->get("lonelythreads_entry")."\";");
}

eval("\$lonelythreads = \"".$templates->get("lonelythreads")."\";");
output_page($lonelythreads);
?>

SCREENSHOT COLOR CHART:
Blue line down = Correct order by last post date, last post date and last post user still show incorrectly but the order by is correct.
Red lines = Indicates the last user to post on the board. As you can see it replicates the timestamp and the user over several threads.
Yellow highlight = Last posted thread/post which is where the red lines are picking up timestamp and name from.



https://caeleste-rpg.net/lonelythreads.php