分页页数

时间:2014-10-29 20:33:23

标签: php pagination

我正在创作分页。我有一个分页课:

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;   
    }
}

我也像这样使用它:

<?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> ";
        }
    }

?>

输出是&#34;之前的1 2 3(然而很多页面)下一个&#34;。 我想要做的就是这样做,如果有超过十页的话会有&#34; ...&#34;最后一个链接将带您到第11-20页(&#34;之前的11 12 13下一个&#34;)等等。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您不仅要显示以0开头的下10页,还要考虑当前页面 - 然后在病房中走10页,并在显示10个页面按钮后中断迭代:

    $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> ";
        }
    }