Last Updated: February 25, 2016
·
309
· svartedauden

Get Next and Previous Links with MySQL and PHP

Simple code for adding next and previous links to navigate between records.

$navquery= "SELECT(
    SELECT id FROM table 
    WHERE id < '".$current_id."' ORDER BY id DESC LIMIT 1
) AS previd, (
    SELECT id FROM table
    WHERE  id > '".$current_id."' ORDER BY id ASC LIMIT 1
) AS nextid";    

$navresult = mysql_query($navquery);
$navrow = mysql_fetch_array($navresult);
$previd  = $navrow['previd'];
$nextid  = $navrow['nextid'];

echo "Previous ID = ".$previd." - Next ID = ".$nextid;