我正在尝试学习PHP分页。目前,我可以通过数字进行分页,但是上一页/下一页按钮出现在每个页面数字的左侧和右侧,就像这样。
以下是我用于生成分页的代码:
<!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>
1
。最后一页是$total_pages
。将它们相加有什么问题? - u_mulder