嗨我使用简单的分页代码。问题是我的分页中每个页面的序列号从1开始,但我需要从第2页的11-20到第3页的21-30这样上。
我的代码是
$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> ";
};
请有人帮助我。
答案 0 :(得分:1)
使用此
$i=$start_from;
$start=0;
while($row = $result->fetch_array()) {
echo "<tr><td>" .++$i. "</td>" ;
答案 1 :(得分:0)
请检查我的代码:
$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> ";
};
答案 2 :(得分:0)
$start = ($num_rec_per_page * ($page-1))+1;
您必须为$start
尝试使用此方法,以便在第二页上获得11-20,在第三页上获得21-30。