m3tsys m3tsys - 3 years ago 74
MySQL Question

How to build next and previous links with php?

I use this code to get the informations about a certain id

$sql = dbquery("SELECT * FROM `videos` WHERE `id` = ".$local_id." ");

while($row = mysql_fetch_array($sql)){
$video_id = $row["id"];
$video_title = $row["title"];

Let's say the link of a page would be example.com/video.php?id=34

How can i get the next and previous $video_id and $video_title depending on the current id?

A problem is that i can't increase or decrease the value of the current id by 1 because the 35 or 33 may be deleted in the meanwhile...

How can i achieve this?


I have a very big problem: the previous link sends me to the right link but the next link always sends me to the last video added in the database.

If i go to the last or first videos added in the database i get an error because there are no more next and previous videos added.

Answer Source

Perhaps two more queries would work ...

 select id,title from videos where id < $local_id order by id desc limit 1
 select id,title from videos where id > $local_id order by id asc limit 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download