分頁程式設計理解page.class.php

來源:互聯網
上載者:User
關於分頁程式,我的理解是當點擊某個連結如:[下一頁]就顯示下一頁對應的資料.

這本質是對資料庫的操作,簡而言之換成SQL語句就是 select some_fields from table limit Offset Num;

這裡Num是固定的,比如每頁顯示10條資料

關鍵是Offset,即位移量,每點擊下一頁時候,這個值就會發生變化;如:0,10 10,10 20,10....

在頁面上最終顯示的只有兩個東西,一個是對應的資料(圖片,文字等...),一個是 分頁連結(共xx條記錄,每頁顯示10條,當前第1/32頁 [首頁] [上頁] [下頁] [尾頁] )

下面具體實現代碼

count=$count;         $this->meiye=$meiye;        $this->pages=ceil($this->count/$this->meiye);        $this->now_page=$this->get_now_page();        $this->url=$url;        $this->prev=$this->get_prev_page();        $this->next=$this->get_next_page();        $this->offset=$this->get_offset();        $this->style=$this->get_style($style);    }    //獲得當前是第幾頁    private function get_now_page(){        return isset($_GET['page'])?$_GET['page']:1;    }    //獲得上一頁    private function get_prev_page(){        return $this->now_page-1?$this->now_page-1:false;    }    //獲得下一頁    private function get_next_page(){        return $this->now_page+1>$this->pages?false:$this->now_page+1;    }    //獲得位移量    private function get_offset(){        return $this->now_page<=1&&$this->now_page>0?0:($this->now_page-1)*($this->meiye);    }    //獲得分頁連結風格預設是:    //共30條記錄,每頁顯示4條,當前第1/8頁 [首頁] [下一頁] [尾頁]    private function get_style($s){        $str='';        if ($this->pages>1) {            if($s==1){                $str.="共{$this->count}條記錄,每頁顯示{$this->meiye}條,當前第{$this->now_page}/{$this->pages}頁 ";                $str.="url?page=1'>[首頁] ";                $this->prev?$str.="url?page=$this->prev'>[上一頁] ":false;                 $this->next?$str.="url?page=$this->next'>[下一頁] ":false;                 $str.="url?page=$this->pages'>[尾頁]";             }else{                $str.= "當前第{$this->now_page}/{$this->pages}頁";                $str.="url?page=1'>[首頁] ";                $this->prev?$str.="url?page=$this->prev'>[上一頁] ":false;                 for ($j = 1; $j <= $this->pages; $j++) {                    $str.="url?page=$j>$j  ";                }                $this->next?$str.="url?page=$this->next'>[下一頁] ":false;                 $str.="url?page=$this->pages'>[尾頁]";             }            return $str;        }    }}

當使用的時候,取出位移量Offset,和style

acount();    //執行個體化分頁類(總條數,每頁顯示多少條記錄,執行指令碼,樣式)    $p=new page($c,4,'page3.php',1);    //獲得位移量    $of=$p->offset;    //查詢資料庫    $res=$m->select('pic','','',"$of,4");    //便利結果集$res    //輸出分頁連結    echo $p->style;
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.