I have taken the working code from this thread:
Creating Next and Previous buttons for navigation
It works for me but right now I would like to ask is there any way to disable the link if it reaches the 1st or the last ID?
Here’s my brief code:
function getNavID($id) {
$result4= mysql_query("SELECT
( SELECT id FROM products_list WHERE id > '$id' LIMIT 1 )
AS nextValue,
( SELECT id FROM products_list WHERE id < '$id' ORDER BY id DESC LIMIT 1 )
AS prevValue
FROM products_list");
if ($resultID = mysql_fetch_array($result4)) {
return $resultID;
}
else {
return NULL;
}
}
$LinkID = getNavID($id);
<p><a href="update.php?id=<?php echo urlencode($LinkID['prevValue']); ?>">Previous</a></p>
<p><a href="update.php?id=<?php echo urlencode($LinkID['nextValue']); ?>">Next</a></p>
Let’s say the page I’m on now is ID=1 so logically, there is no ID lesser than 1. Is there any way that I can do to disable the links if the ID reaches the min and max ID ?
Thanks in advance guys.
Regards,
Jeff
For ID=1,
$LinkID['prevValue']should return NULL, which you can check easily. Include a check, so that you only print the link if there is a previous/next value, e.g.:And analogously for Next entry.
By the way, just for completeness’ sake, I would rewrite the SQL statement like this:
I.e. an additional LIMIT 1 at the end. You might not notice a difference in your result, but with your statement, the database delivers one row per entry in the table products_list (of which you only use the first, and they are all equal).