<?php
global $con;
$get_mov = "SELECT * FROM movies";
$run_mov = mysqli_query($con, $get_mov);
$number_of_movies = mysqli_num_rows($run_mov);
$number_of_pages = ceil($number_of_movies/4);
if (!isset($_GET['page'])) {
$page = 1;
}
else {
$page = $_GET['page'];
}
$this_page_first_result = ($page-1)*4;
$get_mov = "SELECT * FROM movies order by movie_released DESC LIMIT " .
$this_page_first_result. ',' . 4;
$run_mov = mysqli_query($con, $get_mov);
while ($row_mov = mysqli_fetch_array($run_mov)) {
$mov_id = $row_mov['movie_id'];
$mov_desc = $row_mov['movie_desc'];
$mov_title = $row_mov['movie_title'];
$mov_cat = $row_mov['movie_cat'];
echo "
<article id ='post-8045' class='article'>
<h4><a href='single.php?mov_id=$mov_id' style='text-
decoration:none'>$mov_title</a></h4>
<p>$mov_desc</p>
</article>
";
}
?>
<nav class="float-right">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="?
page=<?php $pagep = $page-1; echo $pagep; ?>" aria-label="Previous"><span
aria-hidden="true">«</span></a></li>
<?php
for($page=1;$page<=$number_of_pages;$page++) {
echo "<li class='page-item'><a href='index.php?
page=$page' class='page-link' style='text-
decoration:none;color:black;'>$page</a></li>";
}
?>
<li class="page-item"><a class="page-link" href="?
page=<?php $pagen = $page+1; echo $pagen; ?>" aria-label="Next"><span
aria-hidden="true">»</span></a></li>
</ul>
</nav>
这是我的代码...我要显示的页面太多,所以它们的行很长,例如..1 2 3 4 5 6 7 8 9 10 11 12等。此外,下一个按钮查询也不起作用... 我想将其限制为<< 4 5 6 7 8 9 >> ...而不是... 我搜索了整个网络和youtube,并与我认识的所有同行进行了联系。 更多信息: “电影”是我的数据库名称。
答案 0 :(得分:0)
确定,因此您想限制分页中的页面链接数量。首先,您需要制定您的要求,在您的问题中,您只说“我想将这些限制为<< 4 5 6 7 8 9 >>”,这还不足以满足您的要求。因此,让我们尝试以下规范:
因此,我们现在必须找到一种既简单又可以满足所有这些要求的算法。一种方法是
$start = max(min($k-2, $n-5), 1);
$end = min($start+5, $n);
完整的测试代码:
function print_pagination($n, $k)
{
printf("%d pages, we are on %d:\n", $n, $k);
$start = max(min($k-2, $n-5), 1);
$end = min($start+5, $n);
for ($i=$start; $i<=$end; $i++) {
if ($i==$k)
printf('%d ', $i);
else
printf('<a href="...">%d</a> ', $i);
}
echo("\n");
}
print_pagination(10, 1);
print_pagination(10, 3);
print_pagination(10, 6);
print_pagination(10, 10);
print_pagination(3, 1);
print_pagination(3, 2);
print_pagination(3, 3);