Sorry for the crappy title but it was the best that I could come up with for this. It could be quite easy to someone but I'm wracking my brains out on it.
Background:
I have a script that shows dynamic articles. The rows for each has the author, title, date, the body, etc... The pages (database rows) are indexed by an auto-incremented number upon saving.
I wanted each page to have links at the bottom of just the previous or the next article. Not full-blown pagination as that is done elsewhere. Just a previous link if there were articles written before the one being displayed and a next if it wasn't the most recent article created.
So I used:
$id = $_REQUEST['id'];
$previous_page = $id - 1;
if(mysql_num_rows(mysql_query("SELECT id FROM articles WHERE id = '$previous_page'"))){
echo "<a href=\"page.php?id=$previous_page\">Previous Page</a>";
}else{
echo "<font color=#cccccc>Previous Page</a>";
}
$next_page = $id + 1;
if(mysql_num_rows(mysql_query("SELECT id FROM articles WHERE id = '$next_page'"))){
echo "<a href=\"page.php?id=$next_page\">Next Page</a>";
}else{
echo "<font color=#cccccc>Next Page</a>";
}
This works perfectly fine if every article written were still in the database. But if someone deletes an article, that row is gone along with the article_id (the table's index).
So if the database has a row for articles 1,2,4,6 because article 3 and 5 were deleted then there'd be problems on the page displaying article 4. Even though there are both older and newer added articles the links will not be displayed because the code would be looking for pages 3 and 5 which do not exist.
So I need some help making the code check if there is a row with a lower and/or higher 'id' so that the links are shown if there were earlier or later articles if some have been deleted.
And lastly I'd need to know the number of the next lowest and next highest row to have the correct 'id' in the link.
I will try to clarify more if necessary. Definitely appreciate any help and all help/guidance!