Next and Previous Link on Message Board

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Goat
Forum Newbie
Posts: 11
Joined: Mon Apr 29, 2002 11:12 pm
Contact:

Next and Previous Link on Message Board

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post 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
Goat
Forum Newbie
Posts: 11
Joined: Mon Apr 29, 2002 11:12 pm
Contact:

Post 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>";
?>
User avatar
amnuts
Forum Newbie
Posts: 16
Joined: Fri Jun 14, 2002 1:48 pm
Contact:

Post 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
Goat
Forum Newbie
Posts: 11
Joined: Mon Apr 29, 2002 11:12 pm
Contact:

Post 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...
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

by doing it the way andy suggested with the offset and limit query you will avoid this problem...

Cheers Sam
Post Reply