萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php中分頁原與與分頁類

php中分頁原與與分頁類

所謂分頁顯示,也就是將數據庫中的結果集人為的分成一段一段的來顯示,這裡需要兩個初始的參數: 每頁多少條記錄($PageSize)?

當前是第幾頁($CurrentPageID)?
現在只要再給我一個結果集,我就可以顯示某段特定的結果出來。
至於其他的參數,比如:上一頁($PReviousPageID)、下一頁($NextPageID)、總頁數($numPages)等等,都可以根據前邊這幾個東西得到。
以MySQL數據庫為例,如果要從表內截取某段內容,sql語句可以用:select * from table limit offset, rows。看看下面一組sql語句,嘗試一下發現其中的規率。
前10條記錄:select * from table limit 0,10
第11至20條記錄:select * from table limit 10,10
第21至30條記錄:select * from table limit 20,10
……
這一組sql語句其實就是當$PageSize=10的時候取表內每一頁數據的sql語句,我們可以總結出這樣一個模板:
select * from table limit ($CurrentPageID – 1) * $PageSize, $PageSize
拿這個模板代入對應的值和上邊那一組sql語句對照一下看看是不是那麼回事。搞定了最重要的如何獲取數據的問題以後,剩下的就僅僅是傳遞參數,構造合適的sql語句然後使用php從數據庫內獲取數據並顯示了。以下我將用具體代碼加以說明。
3、簡單代碼
請詳細閱讀以下代碼,自己調試運行一次,最好把它修改一次,加上自己的功能,比如搜索等等。
[php]

 代碼如下 復制代碼

<?php
// 建立數據庫連接
$link = mysql_connect(“localhost”, “mysql_user”, “mysql_passWord”)
or die(“Could not connect: ” . mysql_error());
// 獲取當前頁數
if( isset($_GET['page']) ){
$page = intval( $_GET['page'] );
}
else{
$page = 1;
}
// 每頁數量
$PageSize = 10;
// 獲取總數據量
$sql = “select count(*) as amount from table”;
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$amount = $row['amount'];
// 記算總共有多少頁
if( $amount ){
if( $amount < $page_size ){ $page_count = 1; }               //如果總數據量小於$PageSize,那麼只有一頁
if( $amount % $page_size ){                                  //取總數據量除以每頁數的余數
$page_count = (int)($amount / $page_size) + 1;           //如果有余數,則頁數等於總數據量除以每頁數的結果取整再加一
}else{
$page_count = $amount / $page_size;                      //如果沒有余數,則頁數等於總數據量除以每頁數的結果
}
}
else{
$page_count = 0;
}
// 翻頁鏈接
$page_string = ”;
if( $page == 1 ){
$page_string .= ‘第一頁|上一頁|’;
}
else{
$page_string .= ‘<a href=?page=1>第一頁</a>|<a href=?page=’.($page-1).’>上一頁</a>|’;
}
if( ($page == $page_count) || ($page_count == 0) ){
$page_string .= ‘下一頁|尾頁’;
}
else{
$page_string .= ‘<a href=?page=’.($page+1).’>下一頁</a>|<a href=?page=’.$page_count.’>尾頁</a>’;
}
// 獲取數據,以二維數組格式返回結果
if( $amount ){
$sql = “select * from table order by id desc limit “. ($page-1)*$page_size .”, $page_size”;
$result = mysql_query($sql);

while ( $row = mysql_fetch_row($result) ){
$rowset[] = $row;
}
}else{
$rowset = array();
}
// 沒有包含顯示結果的代碼,那不在討論范圍,只要用foreach就可以很簡單的用得到的二維數組來顯示結果
?>

PHP分頁器制作,自動生成分面頁碼,JS調用函數

 代碼如下 復制代碼

<?php
class PageView{
    /**頁碼**/
    public $pageNo = 1;
    /**頁大小**/
    public $pageSize = 20;
    /**共多少頁**/
    public $pageCount = 0;
    /**總記錄數**/
    public $totalNum = 0;
    /**偏移量,當前頁起始行**/
    public $offSet = 0;
    /**每頁數據**/
    public $pageData = array();

    /**是否有上一頁**/
    public $hasPrePage = true;
    /**是否有下一頁**/
    public $hasNextPage = true;

    public $pageNoList = array();

    public $jsFunction ='jsFunction';
    /**
     *
     * @param unknown_type $count 總行數
     * @param unknown_type $size 分頁大小
     * @param unknown_type $string
     */
    public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){

        $this->totalNum = $count;//總記錄數
        $this->pageSize = $size;//每頁大小
        $this->pageNo = $pageNo;
        //計算總頁數
        $this->pageCount = ceil($this->totalNum/$this->pageSize);
        $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
        //檢查pageNo
        $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
        $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;

        //計算偏移
        $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
        //計算是否有上一頁下一頁
        $this->hasPrePage = $this->pageNo == 1 ?false:true;

        $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;

        $this->pageData = $pageData;
        $this->jsFunction = $jsFunction;

    }
    /**
     * 分頁算法
     * @return
     */
    private function generatePageList(){
        $pageList = array();
        if($this->pageCount <= 9){
            for($i=0;$i<$this->pageCount;$i++){
                array_push($pageList,$i+1);
            }
        }else{
            if($this->pageNo <= 4){
                for($i=0;$i<5;$i++){
                    array_push($pageList,$i+1);
                }
                array_push($pageList,-1);
                array_push($pageList,$this->pageCount);

            }else if($this->pageNo > $this->pageCount - 4){
                array_push($pageList,1);

                array_push($pageList,-1);
                for($i=5;$i>0;$i--){
                    array_push($pageList,$this->pageCount - $i+1);
                }
            }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
                array_push($pageList,1);
                array_push($pageList,-1);

                array_push($pageList,$this->pageNo -2);
                array_push($pageList,$this->pageNo -1);
                array_push($pageList,$this->pageNo);
                array_push($pageList,$this->pageNo + 1);
                array_push($pageList,$this->pageNo + 2);

                array_push($pageList,-1);
                array_push($pageList,$this->pageCount);

            }
        }
        return $pageList;
    }

    /***
     * 創建分頁控件
    * @param
    * @return String
    */
    public function echoPageAsDiv(){
        $pageList = $this->generatePageList();

        $pageString ="<div class='pagination'><div class='page-bottom'>";

        if(!empty($pageList)){
            if($this->pageCount >1){
                if($this->hasPrePage){
                    $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一頁</a>";
                }
                foreach ($pageList as $k=>$p){
                    if($this->pageNo == $p){
                        $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
                        continue;
                    }
                    if($p == -1){
                        $pageString = $pageString ."<span class='page-break'>...</span>";
                        continue;
                    }
                    $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
                }

                if($this->hasNextPage){
                    $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一頁</a>";
                }

            }
        }
        $pageString = $pageString .("</div></div>");
        return $pageString;
    }
}

?>

<style type="text/css">
<!--
.pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
.pagination-tab { margin-bottom: 20px;}
.pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
    display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
.pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
    border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
.pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
.pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
.pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
.pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
.pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
.pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
.pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
.pagination .page-break {border: medium none; background:none transparent; color:#333;}

-->
</style>

PHP調用

 代碼如下 復制代碼


$pageNo = $_GET['pageNo'];
if(empty($pageNo)){
}
//分頁數據
$pageData = News::getNewsPage($pageNo,$pageSize);
       //取得總行數
$count = News::getNewsCount();
        //創建分頁器
$p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
     //生成頁碼
$pageViewString = $p->echoPageAsDiv();

copyright © 萬盛學電腦網 all rights reserved