PHP-MySql: Select previous row and next row from current ID

Written on July 15, 2008 – 5:15 pm | by vandai |

During the dark age, the simplest way to get previous row from current ID was using this query:

  1. SELECT field1
  2. FROM tablename
  3. WHERE id = ($currentId - 1)

and to get next row:

  1. SELECT field1
  2. FROM tablename
  3. WHERE id = ($currentId + 1)

But there’s a problem. Like common data, we need to delete some rows. For example, the current ID is 14 ($currentid = 14) , and i deleted row id number 13, when i run the query to get the previous row, the result will empty. The sql cannot find ID number 13 ($currentid - 1).

How to fix it?
Again, the first simplest way i got is to loop the ID. When it cannot find the first query, it calculate again by looping it with PHP.

//the bad way..
  1. <?
  2. $ok = false;
  3. $prev = $currentid - 1;
  4. while($ok == false){
  5.    $sql = "select * from tablename where id = $prev ";
  6.    $data = $db->query($sql);
  7.    if(empty($data)){
  8.       $prev = $prev-1;
  9.    }else{
  10.       $ok = true;
  11.    }
  12. }
  13. ?>

This is really bad idea. It will consume more resources by continually query the database. What happen when you deleted 10 rows from current ID?
There will be 10 looping with 10 query to database. How about deleted 100 rows?? You must be insane doing this.


So what’s the solution?
We’ll not using the equal ( = ) operator since we must find the exact ID first. We will use the smaller and larger operator ( < > ) and combine it with “limit”.

Query to get the previous row:

  1. SELECT field1
  2. FROM tablename
  3. WHERE id < $currentid
  4. ORDER BY id DESC
  5. LIMIT 1

And the next row:

  1. SELECT field1
  2. FROM tablename
  3. WHERE ID > $currentid
  4. ORDER BY id ASC
  5. LIMIT 1

Simply, find the smaller ID from the current ID, sort it by descending, and limit by 1 to ensure we got the exactly previous row. And the same way for the next row. Get the larger ID from the current ID, sort it by ascending, and limit by 1.

No more resource wasting.

  1. 4 Responses to “PHP-MySql: Select previous row and next row from current ID”

  2. By bak wan on Aug 4, 2008 | Reply

    sayangnya trik ini tidak bisa diterapkan jika ID bukan bertipe data numerik (integer, float, dll)

    solusinya mungkin seperti ini :

    kita harus tahu di posisi row keberapakah data yang saat ini ditampilkan, simpan ke variabel misal $currentRow

    kode :

    $goNext = $currentRow + 1;
    $goPrev = $currentRow - 1;

    kemudian link untuk navigasinya jadi seperti ini :
    Prev
    Next

    gunakan passing variabel $currentRow dari navigasi untuk query berikut :

    SELECT * FROM something LIMIT $currentRow, 1

    selengkapnya >>

  3. By Adrian on Aug 18, 2008 | Reply

    Thank you so much for this. I’ve been looking for a more efficient way of doing this for some time. Its a simple, elegant solution.

  4. By Martin on Aug 29, 2008 | Reply

    Thank’s a lot. Indeed it is a elegant solution. Only one problem: if I have reached the last row (or the first row) and then I put the next-button (previous button) I get a empty form mask. How can I avoid this?

    Two ideas (logically, no technical):
    1. If I reached the last row (last dataset) the next button is disabled.
    What is the technical way to program this?

    2. If I reached the last row pushing the next-button fetches again the last row.
    How can I program this?

    Thanks a lot for all helpful suggestions

    Martin

  5. By vandai on Sep 4, 2008 | Reply

    1. Add this:
    $data = (your sql query);
    if(empty($data))
    echo “<input type=’button’ value=’next’ disabled=’true’ />”;

    2. Add more query. or add ‘if-else’ case on your query.

Post a Comment

About Me

Here I'll share my knowledge, discovery and experience related to my hobby and work. Most articles on this site are related to daily life, hobbies, programming, and linux. More

Want to subscribe?

 Subscribe in a reader
Find entries :