douyan3478
2016-03-13 19:36 阅读 125
已采纳

无法显示:我的分页功能中的活动页面

am using following function to display pagination

public function paginationLinks(){
  $outputString = "";
  $q = $this->db->query('SELECT COUNT(*) FROM videos');
  $res = $q->fetch();
  $newsNumber = $res[0];
  $q->closeCursor();
  for($i = 1; $i <= ceil($newsNumber / $this->newsByPage); $i++){
    $outputString .="<li><a href='?page=" . $i . "'>" . $i . "</a></li> ";
  }
  return $outputString;
}    

and this is how i display

<ul class="pagination pagination-sm">
  <?php echo $news->paginationLinks(); ?>
</ul>

now bootstrap doesn't display active page its because of function

how do i add some more option like next, previous and :active

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

2条回答 默认 最新

  • 已采纳
    doudao1369 doudao1369 2016-03-30 12:09

    Please try this .I hope this help you

    public function paginationLinks(){
    
        $num_rec_per_page   =   10;
        $outputString = "";
    
        $page       = (isset($_GET['page']) && $_GET['page'] != '') ? trim($_GET['page']) : 1;
        $start_from = ($page-1) * $num_rec_per_page; 
    
        $q              = $this->db->query('SELECT COUNT(*) FROM videos');
        $res            = $q->fetch();
        $total_records  = $res[0];
        $q->closeCursor();
    
        $total_pages   = ceil($total_records / $num_rec_per_page); 
    
        $outputString .= "<li><a href='?page=1'>".'|<'."</a></li>"; // Goto 1st page
    
        if($page > 1){
            $prev          = $page - 1;
            $outputString .= "<li><a href='?page=".$prev."'>Prev</a></li>"; // Goto previous page
        }
    
        for ($i=1; $i<=$total_pages; $i++) { 
            $activeClass    =   ($page == $i) ? 'active' : '';
            $outputString .="<li class=".$activeClass."><a href='?page=" . $i . "'>" . $i . "</a></li> ";
        }
    
        if($page < $total_pages){
            $next          = $page + 1;
            $outputString .= "<li><a href='?page=".$next."'>Next</a></li>"; // Goto Next page
        }
        if($page > 1){
            $outputString .= "<li><a href='?page=".$total_pages."'>".'|>'."</a></li>"; // Goto last page
        }
        return $outputString;
    }
    
    点赞 评论 复制链接分享
  • dongwei5794 dongwei5794 2016-03-28 13:24

    just replace your function with this one

    public function paginationLinks(){
      $outputString = "";
      $crpage = isset($_GET['page']) && trim($_GET['page']) != ''?trim($_GET['page']):1;
      $q = $this->db->query('SELECT COUNT(*) FROM videos');
      $res = $q->fetch();
      $newsNumber = $res[0];
      $q->closeCursor();
      for($i = 1; $i <= ceil($newsNumber / $this->newsByPage); $i++){
        if($crpage == $i){
          $outputString .="<li class='active'><a href='?page=" . $i . "'>" . $i . "</a></li> ";
        }else{
          $outputString .="<li><a href='?page=" . $i . "'>" . $i . "</a></li> ";
        }
      }
      return $outputString;
    }
    

    For next or previous see that link great example for PHP pagination.

    点赞 评论 复制链接分享

相关推荐