Numbering Items does not continue with pagination

Hey folks,

I have a table that fetched items out of the database and is paginated. I wanted to include a number next to every item to make it more viewer friendly.
Here is the code:


if(!isset( $_GET['p']) ){$_GET['p'] =0;}
  $per_page=  2;
  $sql= "SELECT * FROM items" ;
  $rows= mysql_num_rows ( mysql_query ($sql)) ;
  $pages= ceil($rows / $per_page);
$sql2 = "SELECT * FROM items WHERE `active` = 1 LIMIT ".$_GET['p'].",". $per_page;
$query= mysql_query ($sql2) ;

$n =1
while ( $row = mysql_fetch_assoc ( $query ) ){
echo $row['item'], $row['item2'], $n++;
}
for ( $i=0; $i<$pages; $i++){
   echo ($i == $_GET['p'] / $per_page) ' <a href="index.php?p=' . ($i * $per_page) . '>'.($i +1). '</a>';
	}

The code works fine, but when I go to the next page the numbers restart from 1.
I tried several things such as

if ($_GET['p'] == 0){$n = 1}else{$n =6} 

Which would make sense if 5 items are displayed perpage.

The problem is that since pressing page 2 does not refresh the site but only the table i am guessing the if statement only gets read once.

Any ideas how to easily solve this problem?

Thanks you very much :slight_smile:

If you know pagenumber p, and you have n items per page, then the count should start from

((p - 1) * n) + 1

Thank you guido,

The formula didn’t work exactly but it made me realize that I can just plug a function in instead of $n and that made it very easy.

Thanks again :slight_smile: