douwen9540
douwen9540
2018-06-21 00:40

限制分页中显示的页数

已采纳
  <?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>

This is my code...I have too many pages to be shown so they are making a long line like..1 2 3 4 5 6 7 8 9 10 11 12 and so on. Moreover the next button query is also not working... I want to limit those as << 4 5 6 7 8 9 >> ...instead of that... I Searched the whole web and youtube and contacted all peers i know..but found nothing... More info: "movies" is my database name.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douyin7416 douyin7416 3年前

    OK, so you want to limit the number of page links in your pagination. First of all, you need to formulate your requirement, in your question you only say "I want to limit those as << 4 5 6 7 8 9 >>" which is not a sufficient specification to work with. So let's try with this specification:

    • We want a maximum of 6 page links
    • When we are on the first and second page, the links should be << 1 2 3 4 5 6 >>
    • When we are on page k, the links should be << k-2 k-1 k k+1 k+2 k+3 >>
    • Unless k<3, where we want to start with 1
    • Unless k>n-3, with n the total number of pages, then we want << n-5 n-4 n-3 n-2 n-1 n >>

    So now we have to find an algorithm that's simple enough, yet fulfils all those requirements. One way to do this would be

    $start = max(min($k-2, $n-5), 1);
    $end = min($start+5, $n);
    

    Full code with test:

    function print_pagination($n, $k)
    {
      printf("%d pages, we are on %d:
    ", $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("
    ");
    }
    
    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);
    
    点赞 评论 复制链接分享

相关推荐