Page 1 of 1

Next and Previous Link on Message Board

Posted: Fri Jun 14, 2002 11:12 pm
by Goat
I've got a message board and I'm trying to add a Next and Previous thread link into the whole mix. I've got the variable $thread which is auto_increment and unique to every message. What I want to do is to have the current message take the current $thread from the MySQL and then check to see if there is another thread after that. If there is a next thread then it will display a link to the thread. If the next thread doesn't exist it would only display a text message or nothing at all.

I've tried creating a function that would do the whole thing, but it never seemed to work (which i believe is due to my newness to PHP). If someone could direct me to a tutorial or an example of something like this I would greatly appreciate it.

Posted: Sat Jun 15, 2002 3:16 am
by volka
there may be a smarter/faster way but you may request the highest thread-id in the table

Code: Select all

$sql = 'SELECT MAX(thread) from news';
then you check the current thread-id against the max-thread-id.

Posted: Sat Jun 15, 2002 12:39 pm
by amnuts
Hi,

I would personally use the LIMIT function in MySQL to get a certain amount of records. To do this, use a couple variables in PHP, one to tell you what number to start at and the other to tell you how many to get. Such as:

Code: Select all

$start_at = 0;
$get_number = 15;
Then do a SQL call like this:

Code: Select all

SELECT * FROM table LIMIT $start_at,$get_number
Then to do you previous/next links you just pass the variables along, incrementing/decrementing as needed, such as:

Code: Select all

$previous = $start_at - $get_number;
$next = $start_at + $get_number;

echo "<a href="$PHP_SELF?start_at=$previous&get_number=$get_number">previous</a><br>\n";
echo "<a href="$PHP_SELF?start_at=$next&get_number=$get_number">next</a><br>\n";
Something like that... Might require a little tweaking along the way. :)

Andy

Posted: Sun Jun 16, 2002 2:39 am
by Goat
Thanks for your help, here's the finished working product I came up with...

Code: Select all

<?
	$query1 = MYSQL_QUERY("SELECT max(thread) as max_thread FROM $table");
	$getvar = MYSQL_FETCH_ARRAY($query1);
	$maxthread = $getvar&#1111;"max_thread"]; 
	$next = $id + 1;
	
	if($id == $maxthread)
	echo "Next Thread";
	else
	echo "<a href="message.php3?id=$next">Next Thread</a>";
?>

Posted: Sun Jun 16, 2002 1:18 pm
by amnuts
Hi,

Just a little warning... Incrementing the id by one might result in a broken script. For example; if you have the id to auto_increment and you add the rows (id, name) such as:

1, foo
2, bar
3, blah

and then you delete row where id==2, you're left with the rows with ids 1 and 3. So you're looking at row 1 and the max() is 3. The 'next thread' will be set as 2 - but that row doesn't exist anymore.

If you are positive that will never happen, then okay. But it's something you have to be wary of.

Andy

Posted: Sun Jun 16, 2002 4:06 pm
by Goat
shiznit. I didn't think of that. I guess I'll have to do some more research and come up with a solution...

Posted: Sun Jun 16, 2002 5:56 pm
by sam
by doing it the way andy suggested with the offset and limit query you will avoid this problem...

Cheers Sam