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.
Next and Previous Link on Message Board
Moderator: General Moderators
there may be a smarter/faster way but you may request the highest thread-id in the tablethen you check the current thread-id against the max-thread-id.
Code: Select all
$sql = 'SELECT MAX(thread) from news';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:
Then do a SQL call like this:
Then to do you previous/next links you just pass the variables along, incrementing/decrementing as needed, such as:
Something like that... Might require a little tweaking along the way. 
Andy
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;Code: Select all
SELECT * FROM table LIMIT $start_at,$get_numberCode: 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";Andy
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ї"max_thread"];
$next = $id + 1;
if($id == $maxthread)
echo "Next Thread";
else
echo "<a href="message.php3?id=$next">Next Thread</a>";
?>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
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