Reputation:
Hi iam using simple pagination code .It getting the problem is that the serial number start from 1 in every page in my pagination ,but i need it from 11-20 in 2nd page, from 21-30 in 3rd page and so on.
my code is
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page+1;
$sql = "SELECT * FROM users LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
?>
<table>
<tr><td>SNo</td><td>Name</td><td>Phone</td></tr>
<?php
$i=1;
$start=0;
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr><td><?php echo $i+$start; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['subject']; ?></td>
</tr>
<?php
$i++;
};
?>
</table>
<?php
$sql = "SELECT * FROM users";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result);
$total_pages = ceil($total_records / $num_rec_per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='sample.php?page=".$i."'>".$i."</a> ";
};
Please anyone help me.
Upvotes: 0
Views: 4467
Reputation: 21
$start = ($num_rec_per_page * ($page-1))+1;
You have to try this for your $start
so you get 11-20 on the 2nd page, 21-30 on the 3rd page.
Upvotes: 0
Reputation: 509
use this
$i=$start_from;
$start=0;
while($row = $result->fetch_array()) {
echo "<tr><td>" .++$i. "</td>" ;
Upvotes: 1
Reputation: 519
Please check my code :
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page+1;
$sql = "SELECT * FROM users LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
?>
<table>
<tr><td>SNo</td><td>Name</td><td>Phone</td></tr>
<?php
$i= $start_from;
$start=0;
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr><td><?php echo $i+$start; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['subject']; ?></td>
</tr>
<?php
$i++;
};
?>
</table>
<?php
$sql = "SELECT * FROM users";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result);
$total_pages = ceil($total_records / $num_rec_per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='sample.php?page=".$i."'>".$i."</a> ";
};
Upvotes: 0