如何进行动态页码分页

时间:2019-02-22 11:41:15

标签: php pagination

我的分页效果很好,唯一的问题是我只能显示它并有固定数量的页面可供选择。现在我把它放在10页上,但是我希望我根据$ total_records2动态地进行更改。

所以可以说我有100条记录,限制pr页是5($ limit2 = 5;) 将有20页。现在我只能以这种方式显示它1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20。

但是我想要的是根据记录和您所在的页面动态更改分页。

所以看起来更像这样:

在第1页: 1 2 3 4 5 6 7 8 9 10最后

在第9页: 前4 5 6 7 8 9 10 11 12 13最后

在第20页: 前11 12 13 14 15 16 17 18 19 20

我尝试了很多事情,但似乎找不到正确的解决方案。 有人可以告诉我如何使用当前的代码吗?

谢谢

    <?
        $limit2 = 5;

        $total_records2 = count(user_ended_tips());
        $total_pages2 = ceil($total_records2 / $limit2);
        if (isset($_GET["page2"])) { $page2  = $_GET["page2"]; } else { $page2=1; };  
        $start_from2 = ($page2-1) * $limit2;   
    ?>

    <div class="col-md-6">

      <div id="target-content2" >loading...</div>


      <div class="row">
          <div class="col-md-12 text-center">
          <ul class='pagination text-center' id="pagination2">
            <?php if(!empty($total_pages2)):for($j=1; $j<=10; $j++):  
            if($j == $page2):?>
                  <li class='active' id="<?php echo $j;?>"><a href='pagination_ended_user_tips?page2=<?php echo $j;?>' target='target-content2'><?php echo $j;?></a></li> 
            <?php else:?>
            <li id="<?php echo $j;?>"><a href='pagination_ended_user_tips?page2=<?php echo $j;?>'><?php echo $j;?></a></li>
            <?php endif;?>      
            <?php endfor;endif;?> 
          </ul>
        </div>
      </div>

<script type="text/javascript">
$(document).ready(function() {
$("#target-content2").load("pagination_ended_user_tips?page2=1");
    $("#pagination2 li").click(function(e){
    e.preventDefault();
    $("#target-content2").html('loading...');
    $("#pagination2 li").removeClass('active');
    $(this).addClass('active');
        var pageNum = this.id;
        $("#target-content2").load("pagination_ended_user_tips?page2=" + pageNum);
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

尝试一下:

<?php
        $limit2 = 5;

        $total_records2 = count(user_ended_tips());

        if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  

    ?>

  <div class="col-md-6">

    <div id="target-content2">loading...</div>


    <div class="row">
      <div class="col-md-12 text-center">
        <ul class='pagination text-center' id="pagination2">

          for($j=0; $j<=total_records2; $j++){  
             <li class='<?Php echo $page==$j?'active':'';' id="<?php echo $j;?>">
            <a href='pagination_ended_user_tips?page=<?php echo $j;?>' target='target-content2'>
              <?php echo $j;?>
            </a>
          </li>
          <?php } ?>
        </ul>
      </div>
    </div>

    <script type="text/javascript">
      $(document).ready(function() {
        $("#target-content2").load("pagination_ended_user_tips?page2=1");
        $("#pagination2 li").click(function(e) {
          e.preventDefault();
          $("#target-content2").html('loading...');
          $("#pagination2 li").removeClass('active');
          $(this).addClass('active');
          var pageNum = this.id;
          $("#target-content2").load("pagination_ended_user_tips?page2=" + pageNum);
        });
      });
    </script>