# PHP pagination : records with non-consecutive id numbers (?)

**URL:** <https://forum.kirupa.com/t/php-pagination-records-with-non-consecutive-id-numbers/238108>\
**Category:** programming\
**Created:** [September 7, 2007, 8:34am UTC](https://forum.kirupa.com/t/php-pagination-records-with-non-consecutive-id-numbers/238108 "2007-09-07T08:34:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kompuser](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@kompuser](https://forum.kirupa.com/u/kompuser)\
**Post date:** [September 7, 2007, 8:34am UTC](https://forum.kirupa.com/t/php-pagination-records-with-non-consecutive-id-numbers/238108/1 "2007-09-07T08:34:52Z")

</div>

hi all  
I have to set a pagination php code based on this useful script.

```php

if (isset($_GET['id'])) {
  $id = $_GET['id'];
  } else {
  $id = 1;
}
                
  $query = "SELECT count(*) FROM mytable";
  $result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
  $query_data = mysql_fetch_row($result);
  $numrows = $query_data[0];
  $rows_per_page = 1;
  $lastpage = ceil($numrows/$rows_per_page);
  $id = (int)$id;
  
   if ($id < 1) {
      $id = 1;
      } elseif ($id > $lastpage) {
      $id = $lastpage;
  }

```

Then the following are set to create the previous/next links

```php

if ($id == 1) {
    echo " FIRST ";
    } else {
    echo " <a href='$PHP_SELF'?id=1'>FIRST</a> ";
    $prevpage = $id-1;
    echo " <a href='$PHP_SELF?id=$prevpage'>PREV</a> ";
    }
    if ($id == $lastpage) {
    echo " LAST ";
    } else {
    $nextpage = $id+1;
    echo " <a href='$PHP_SELF?id=$nextpage'>NEXT</a> ";
    echo " <a href='$PHP_SELF?id=$lastpage'>LAST</a> ";
    }

```

Then I have some code to retrive the actual data. (not shown here)

The main problem is that , On ‘mytable’ which hold all the records, the ID column - which is SET as Auto-increment, doesn’t show consecutive ID numbers (such as 1,2,3,4…) because of some DELETE done by the backoffice.

So My question is : How to retrieve the ‘next available row id’ stating that it will not ever be the next following number (that’s why $id+1 is causing problem I guess) ?

thanks  
K.
