我需要在下一页旁边输入序列号,第一页从1到10,下一页从11到20 ...但是我的代码为所有页面打印1到10。
<?php
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td><a href=\"view.php?id=$res[id]\">View</a> | <a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
$pageNumber = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$currentNumber = ($pageNumber - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}
?>
答案 0 :(得分:0)
您的代码应该是这样
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$number = 1;
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$total_pages_sql = "SELECT COUNT(*) FROM regs";
$result = mysqli_query($conn,$total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);
//print_r($total_pages);
$result = mysqli_query($conn, "SELECT * FROM regs LIMIT $offset, $no_of_records_per_page");
while($res = mysqli_fetch_array($result)) {
// How many elements per page
echo "<tr>";
echo "<td>".$res['FirstName']."</td>";
echo "<td>".$res['Email']."</td>";
echo "<td>".$res['Gender']."</td>";
echo "<td><a href=\"view.php?id=$res[id]\">View</a> | <a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";
$currentNumber = ($pageno - 1) * $no_of_records_per_page + $number;
echo "<td> ". $currentNumber++ ." </td>";
$number++;
}