PHP 分页 - 下一个/上一个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41777993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:28:12 来源:igfitidea点击:
PHP pagination - next/previous button
提问by Arsalan Imran
I'm trying to learn PHP pagination. So far I can paginate through numbers, but previous/next buttons are appearing on the left and right of each page number, like this.
我正在尝试学习 PHP 分页。到目前为止,我可以对数字进行分页,但是上一个/下一个按钮出现在每个页码的左侧和右侧,如下所示。
Here is the code I am using to generate pagination:
这是我用来生成分页的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>List Of Customers</h2>
<div class="row">
<a href="add.php" class="btn btn-success">Create</a>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Phone</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include 'db.php';
$link = db_connect();
$limit = 4;
if (isset($_GET["page"] ))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
$record_index= ($page-1) * $limit;
$sql = "SELECT * FROM student_form LIMIT $record_index, $limit";
$retval = mysqli_query($link, $sql);
// print_r($retval);
if (mysqli_num_rows($retval) > 0) {
while($row = mysqli_fetch_assoc($retval)) {
echo '<tr>';
echo '<td>'. $row['first_name'] . '</td>';
echo '<td>'. $row['last_name'] . '</td>';
echo '<td>'. $row['address'] . '</td>';
echo '<td>'. $row['phone'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. "<a class='btn btn-primary' href=edit.php?id=".$row["id"]."> Edit </a><br>"."<a class='btn btn-danger' href=delete.php?id=".$row["id"]."> Delete</a><br>";
}
}
else {
echo "0 results";
}
echo "</tbody>";
echo "</table>";
$sql = "SELECT COUNT(*) FROM student_form";
$retval1 = mysqli_query($link, $sql);
$row = mysqli_fetch_row($retval1);
$total_records = $row[0];
// echo $total_records;
$total_pages = ceil($total_records / $limit);
//$pagLink = "<div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<ul class='pagination'>";
echo "<li><a href='index.php?page=".($page-1)."' class='button'>Previous</a></li>";
echo "<li><a href='index.php?page=".$i."'>".$i."</a></li>";
echo "<li><a href='index.php?page=".($page+1)."' class='button'>NEXT</a></li>";
echo"</ul>";
};
//echo $pagLink . "</div>";
mysqli_close($link);
?>
</div>
</div>
</body>
</html>
回答by u_mulder
Move these urls out of for
loop:
将这些网址移出for
循环:
echo "<ul class='pagination'>";
echo "<li><a href='index.php?page=".($page-1)."' class='button'>Previous</a></li>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<li><a href='index.php?page=".$i."'>".$i."</a></li>";
};
echo "<li><a href='index.php?page=".($page+1)."' class='button'>NEXT</a></li>";
echo "</ul>";