MyBB Community Forums

Full Version: Check if we're on the last page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm currently using the following code to check if we're on the last page of a thread.

$thread = get_thread($mybb->input['tid']);
$perpage = $mybb->settings['postsperpage'];
$postcount = intval($thread['replies'])+1;
$pages = $postcount / $perpage;
$pages = ceil($pages);

if($mybb->input['page'] == $pages)
{
	// Example
}

It works fine (except when you're in a URL without the page parameter, such as the lastpost or newpost features - any ideas on that?), but I think it's too much overhead to run a query for a simple thing like this. Is there an easier way to do it?

I'm aware of $mybb->input['page'] == "last" but it only works if we're actually on showthread.php?tid=123&page=last. If I try to go to the last page normally (e.g. showthread.php?tid=123&page=5) it doesn't work.

Thanks. Smile
$thread is already populated in the showthread file, no matter what page you are on that is available to you.

what about

$perpage = $mybb->settings['postsperpage'];
$postcount = intval($thread['replies'])+1;
$pages = $postcount / $perpage;
$pages = ceil($pages);

if($mybb->input['page'] == ' first') $mybb->input['page'] = 1;
if($mybb->input['page'] == 'last') $mybb->input['page'] = $pages;
if(!$mybb->input['page']) $mybb->input['page'] = 1;

if($mybb->input['page'] == $pages)
{
    // Example
} 
Thanks for the reply pavemen! Smile

I imagined so, but it doesn't work. I tried it before too. If I remove $thread = get_thread($mybb->input['tid']); nothing will happen (I did globalise $thread). And if I put it back, it works again. Am I missing something here? I was already doing those if statements by the way, they don't solve the issue in links without the page parameter.
care to share your code? if you want it private, you can PM or email it to me.
Nevermind, just a stupid mistake. It wasn't working because I was hooking the function to global_end (which actually had a purpose, but I figured it was best to do it in another way). I guess that solves it. Thanks again for clearing it up! Smile

I still don't know how to handle threads without the page parameter in the URL. If you access a thread normally from the forumdisplay (e.g. showthread.php?tid=123) I can easily check if we're on the last page using the method I described in the main post. However, if you click a thread from the index in the last post column it is immediately redirected to a link without the page parameter (e.g. showthread.php?tid=123&pid=456#pid456), making it impossible to know which page we're on.

I tried the following code to assume that we were on the last page if we were accessing &action=lastpost, but since you're redirected to another link it doesn't work.

if($mybb->input['action'] == "lastpost")
{
	$mybb->input['page'] = $pages;
}
when do you need to run your code? A combination of my previous post to get the total pages and the code in showthread from line 823 to 838 will get the page the post is on will let you compare and figure out if you are on the last page
Thanks again pavemen. Wink

I grabbed that code from showthread and it fixed the issue. Should I be worried about the query it's running or is it minimal? Also, within that query is a variable called $visible which adds another parameter to the query if we're a moderator (to account for unapproved posts). Do I have to go ahead and grab the code that builds that variable as well (I know where it is), or is it available to me?

This is the code I'm currently using:

if($mybb->input['pid'])
{
	$post = get_post($mybb->input['pid']);
	$query = $db->query("
		SELECT COUNT(p.dateline) AS count FROM ".TABLE_PREFIX."posts p
		WHERE p.tid='".$thread['tid']."'
		AND p.dateline <= '".$post['dateline']."'
		$visible
	");
	$result = $db->fetch_field($query, "count");
	if(($result % $perpage) == 0)
	{
		$mybb->input['page'] = $result / $perpage;
	}
	else
	{
		$mybb->input['page'] = intval($result / $perpage) + 1;
	}
}
when are you trying to run your code? which hook if it is a plugin?
Oh, sorry, it's showthread_start.
where does the output need to be? can you use showthread_end instead? if so, the query is already run for you
Pages: 1 2