我正在尝试学习PHP分页。到目前为止,我可以通过数字分页,但上一页/下一页按钮出现在每个页码的左侧和右侧like this。
以下是我用于生成分页的代码:
<!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>
答案 0 :(得分:4)
将这些网址移出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>";