Reputation: 657
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>';
?>
Upvotes: 2
Views: 674
Reputation: 8101
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 = 2 (second page) $x = 1 + (1*20) = 21
When $next = 3 (third page) $x = 1 + (2*20) = 41
So on...
Upvotes: 1
Reputation: 136
Can you please use limit in your sql query and you use below code for getting limit.
$start = 1 + ((page number)*20);
$end = $start + 19;
Now you can set it in your sql query
$sql = "select * from table name where limit".$start.", ".$end;
Upvotes: 1
Reputation: 6755
try like this
<?php
if(isset($_REQUEST['pn'])){
$pn = $_REQUEST['pn'];
} else {
$pn = '1';
}
$x = $pn * 20; //place your no. of rows instead of 20
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>';
?>
Upvotes: 0