dsn5510 2014-10-29 20:33
浏览 37

分页页数

I am creating pagination. I have a pagination class:

class Pagination {
    public $current_page;
    public $per_page;
    public $total_count;

    public function __construct($page=1, $per_page=10, $total_count=0){
        $this->current_page = (int)$page;
        $this->per_page = (int)$per_page;
        $this->total_count = (int)$total_count;
    }

    public function offset(){
        // Assuming 20 items per page:
        // page 1 has an offset of 0    (1-1) * 20
        // page 2 has an offset of 20   (2-1) * 20
        //  in other words, page 2 starts with item 21
        return ($this->current_page - 1) * $this->per_page; 
    }

    public function total_pages(){
        return ceil($this->total_count/$this->per_page);    
    }

    public function previous_page(){
        return $this->current_page - 1;
    }

    public function next_page(){
        return $this->current_page + 1;
    }

    public function has_previous_page(){
        return $this->previous_page() >= 1 ? true : false;  
    }

    public function has_next_page(){
        return $this->next_page() <= $this->total_pages() ? true : false;   
    }
}

I also am using it like this:

<?php

    // 1. the current page number ($current_page)
    $page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;

    // 2. records per page
    $per_page = 8;

    // 3. total record count ($total_count)
    $total_count = Song::count_all();

    $pagination = new Pagination($page, $per_page, $total_count);

    // Instead of finding all records, just find the records
    // for this page
    $sql     = "SELECT * FROM songs ";
    $sql    .= "ORDER BY dopeness DESC ";
    $sql    .= "LIMIT {$per_page} ";
    $sql    .= "OFFSET {$pagination->offset()}";
    $songs = Song::find_by_sql($sql);
?>



<?php
    if($pagination->total_pages() > 1){
        if($pagination->has_previous_page()){
            echo "<a href=\"all_songs.php?page=";
            echo $pagination->previous_page();
            echo "\">&laquo;Previous&nbsp;&nbsp;</a> ";
        }

        for($i=1; $i<=$pagination->total_pages(); $i++){
            if($i == $page){
                echo " <span class=\"selected\">{$i}</span> ";
            } else {
                echo " <a href=\"all_songs.php?page={$i}\">{$i}</a> ";
            }
        }

        if($pagination->has_next_page()){
            echo "<a href=\"all_songs.php?page=";
            echo $pagination->next_page();
            echo "\">&nbsp;&nbsp;Next&raquo;</a> ";
        }
    }

?>

Output is "previous 1 2 3 (however many pages) next". What I want to do is make it so that if there are more than ten pages there will be "..." for the last link that would take you to page 11-20 ("previous 11 12 13 next") and so on. Can anybody help me to tackle this?

  • 写回答

1条回答 默认 最新

  • drccfl9407 2014-10-29 20:44
    关注

    You should not only display the next 10 pages starting with 0, but consider the current page - then walk 10 pages on wards, and break the iteration, once you displayed 10 page-buttons:

        $page = 5; //assuming within valid bounds
    
        for($i=$page; $i<=$pagination->total_pages(); $i++){
            if ($i > $page+ 10){
               echo " ... ";
               break;
            }
    
            if($i == $page){
                echo " <span class=\"selected\">{$i}</span> ";
            } else {
                echo " <a href=\"all_songs.php?page={$i}\">{$i}</a> ";
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测