dtd14883 2019-01-13 09:23
浏览 47

有没有办法用Javascript显示每页的条目?

Lets say that I have built a file in which all of the entries coming from DB are going to be shown.

In order to avoid doing server-side pagination, I would like to create something like hide/show old/new pages after specific amount of entries found.

This is mainly because I have found myself really struggling with the pagination in the server side which I normally can do but this time its impossible as I'm fetching data from two DB tables.

This is the code that I have done using Javascript(I'm using a library); this is my html:

<div class="page" id="page1">
   <?php foreach($data as $d) :  ?>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
       <div class="col-md-4">.....content.....</div>
   <?php endforeach; ?>
</div>
<ul id="pagination-demo" class="pagination"></ul>

and this is the javascript:

$('#pagination-demo').twbsPagination({
  totalPages: <?= $i++ ?>,
  // the current page that show on start
  startPage: 1,

  // maximum visible pages
  visiblePages: 5,

  initiateStartPageClick: true,

  // template for pagination links
  href: false,

  // variable name in href template for page number
  hrefVariable: '{{number}}',

  // Text labels
  first: 'First',
  prev: 'Previous',
  next: 'Next',
  last: 'Last',

  // carousel-style pagination
  loop: false,

  // callback function
  onPageClick: function (event, page) {
    $('.page-active').removeClass('page-active');
    $('#page'+page).addClass('page-active');
  },

  // pagination Classes
  paginationClass: 'pagination',
  nextClass: 'next',
  prevClass: 'prev',
  lastClass: 'last',
  firstClass: 'first',
  pageClass: 'page',
  activeClass: 'active',
  disabledClass: 'disabled'

});

So assuming that I currently have 20 entries in DB, how can I make it possible to only display 12 per page?

The actual behavior is just showing 20 entries per page.

Thanks for reading?

  • 写回答

1条回答 默认 最新

  • duanru6816 2019-01-13 10:37
    关注

    use offset and limit in your SQL query

    <?  
        $limit = $_GET['limit'];
        $offset = $_GET['offset'];
        $bdd = new PDO(LOGIN DATA);
        $req = $bdd->prepare("SELECT * FROM table LIMIT ? OFFSET ?");
        $req->execute(array($limit, $offset));
    ?>
    

    Then in your page use this loop

    <?php 
      while($line = $req->fetch()){
    ?>
    your html code
    <?php
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义