Amran Amran - 6 months ago 8
MySQL Question

How to display the numbers of each rows in PHP

I am having problem to display the numbers of each rows. I am displaying a table with

LIMIT
of up to
20 rows
. I can display the numbers in the
while loop
but when user click for next rows it will display the number back to 1. How can I display it as a continue number example,
1-20, 21-40, etc
? Below are my codes:

<?php
//SQL AND LIMIT codes here
$x = '1';
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);

echo '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a>';
?>

Answer

As you mentioned $next is page number:

One solution is you can do:

$x = 1 + (($next -1 ) * 20);

So... When $next = 1 (first page) $x = 1 + (0*20) = 1

When $next = 1 (second page) $x = 1 + (1*20) = 21

When $next = 1 (third page) $x = 1 + (2*20) = 41 So on...

Comments