Last Updated: February 25, 2016
·
451
· 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;

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#mysql
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside