php+mysql分頁類

jopen 9年前發布 | 2K 次閱讀 PHP 分頁

封裝的類:

    <?php
/*
類名: PageSupport 功能:分頁顯示MySQL數據庫中的數據
***/
class PageSupport{
//屬性
var $sql; //所要顯示數據的SQL查詢語句
var $page_size; //每頁顯示最多行數

    var $start_index;            //所要顯示記錄的首行序號  
    var $total_records;            //記錄總數   
    var $current_records;        //本頁讀取的記錄數   
    var $result;                //讀出的結果   

    var $total_pages;            //總頁數    
    var $current_page;            //當前頁數  
    var $display_count = 30;     //顯示的前幾頁和后幾頁數  

    var $arr_page_query;        //數組,包含分頁顯示需要傳遞的參數   

    var $first;  
    var $prev;  
    var $next;  
    var $last;  

    //方法  
/*********************************************  
構造函數:__construct() 
輸入參數:             
        $ppage_size:每頁顯示最多行數     
***********************************************/   
 function PageSupport($ppage_size)  
 {   
    $this->page_size=$ppage_size;   
    $this->start_index=0;  
 }   


/*********************************************  
構造函數:__destruct() 
輸入參數:             
***********************************************/   
 function __destruct()  
 {  

 }  

/*********************************************  
get函數:__get() 
***********************************************/   
 function __get($property_name)  
 {    
     if(isset($this->$property_name))   
     {   
            return($this->$property_name);   
     }   
     else   
     {   
            return(NULL);   
     }   
 }  

/*********************************************  
set函數:__set() 
***********************************************/   
 function __set($property_name, $value)   
 {       
    $this->$property_name = $value;   
 }   

/*********************************************  
函數名:read_data 
功能:    根據SQL查詢語句從表中讀取相應的記錄 
返回值:屬性二維數組result[記錄號][字段名] 
***********************************************/   
 function read_data()  
 {   
    $psql=$this->sql;   

    //查詢數據,數據庫鏈接等信息應在類調用的外部實現  
    $result=mysql_query($psql) or die(mysql_error());   
    $this->total_records=mysql_num_rows($result);   

    //利用LIMIT關鍵字獲取本頁所要顯示的記錄  
    if($this->total_records>0)   
    {  
        $this->start_index = ($this->current_page-1)*$this->page_size;  
        $psql=$psql.    " LIMIT ".$this->start_index." , ".$this->page_size;   

        $result=mysql_query($psql) or die(mysql_error());   
        $this->current_records=mysql_num_rows($result);   

        //將查詢結果放在result數組中  
        $i=0;   
        while($row=mysql_fetch_Array($result))  
        {   
            $this->result[$i]=$row;   
            $i++;   
        }   
    }  


    //獲取總頁數、當前頁信息  
    $this->total_pages=ceil($this->total_records/$this->page_size);    

    $this->first=1;  
    $this->prev=$this->current_page-1;  
    $this->next=$this->current_page+1;  
    $this->last=$this->total_pages;  
 }  

 /*********************************************  
函數名:standard_navigate() 
功能:    顯示首頁、下頁、上頁、未頁 
***********************************************/   
 function standard_navigate()   
 {      
    echo "<div align=center>";  
    echo "<form action=".$_SERVER['PHP_SELF']." method=\"get\">";  

    echo "<font color = red size ='4'>第".$this->current_page."頁/共".$this->total_pages."頁</font>";   
    echo "    ";  

    echo "跳到<input type=\"text\" size=\?\" name=\"current_page\" value='".$this->current_page."'/>頁";  
    echo "<input type=\"submit\" value=\"提交\"/>";  


    //生成導航鏈接  
    if ($this->current_page > 1) {  
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->first.">首頁</A>|";   
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->prev.">上一頁</A>|";   
    }  

    if( $this->current_page < $this->total_pages) {  
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->next.">下一頁</A>|";  
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->last.">末頁</A>";   
    }  

    echo "</form>";      
    echo "</div>";  

 }   

  /*********************************************  
函數名:full_navigate() 
功能:    顯示首頁、下頁、上頁、未頁   
生成導航鏈接 如1 2 3 ... 10 11 
***********************************************/   
 function full_navigate()   
 {      
    echo "<div align=center>";  
    echo "<form action=".$_SERVER['PHP_SELF']." method=\"get\">";  

    echo "<font color = red size ='4'>第".$this->current_page."頁/共".$this->total_pages."頁</font>";   
    echo "    ";  

    echo "跳到<input type=\"text\" size=\?\" name=\"current_page\" value='".$this->current_page."'/>頁";  
    echo "<input type=\"submit\" value=\"提交\"/>";  

    //生成導航鏈接 如1 2 3 ... 10 11  
    $front_start = 1;  
    if($this->current_page > $this->display_count){  
        $front_start = $this->current_page - $this->display_count;  
    }  
    for($i=$front_start;$i<$this->current_page;$i++){  
        echo "<a href=".$_SERVER['PHP_SELF']."?page=".$i.">[".$i ."]</a> ";      
    }  

    echo "[".$this->current_page."]";  

    $displayCount = $this->display_count;  
    if($this->total_pages > $displayCount&&($this->current_page+$displayCount)<$this->total_pages){  
        $displayCount = $this->current_page+$displayCount;  
    }else{  
        $displayCount = $this->total_pages;  
    }  

    for($i=$this->current_page+1;$i<=$displayCount;$i++){  
        echo "<a href=".$_SERVER['PHP_SELF']."?current_page=".$i.">[".$i ."]</a> ";      
    }  

    //生成導航鏈接  
    if ($this->current_page > 1) {  
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->first.">首頁</A>|";   
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->prev.">上一頁</A>|";   
    }  

    if( $this->current_page < $this->total_pages) {  
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->next.">下一頁</A>|";  
      echo "<A href=".$_SERVER['PHP_SELF']."?current_page=".$this->last.">末頁</A>";   
    }  

    echo "</form>";      
    echo "</div>";  

 }   

}   
?>  </pre> 


寫在php頁面里面的代碼:

    <div class="index">
<?php

    include_once("fenye_php.php");   //引入類  

    ///////////////////////////////////////////////////////////////////////  
    $con = mysql_connect("localhost","root","");   
    if (!$con)  
      {  
      die('Could not connect: ' . mysql_error());      
      }  

    mysql_select_db("myblog", $con);    //選取數據庫  

    $PAGE_SIZE=10;            //設置每頁顯示的數目  

    ///////////////////////////////////////////////////////////////////////  

    $pageSupport = new PageSupport($PAGE_SIZE); //實例化PageSupport對象  

    $current_page=$_GET["current_page"];//分頁當前頁數  

    if (isset($current_page)) {  

        $pageSupport->__set("current_page",$current_page);  

    } else {  

        $pageSupport->__set("current_page",1);  

    }  


    $pageSupport->__set("sql","select * from article ");       
    $pageSupport->read_data();//讀數據  

    if ($pageSupport->current_records > 0) //如果數據不為空,則組裝數據  
    {  
        for ($i=0; $i<$pageSupport->current_records; $i++)  
        {  
            $title = $pageSupport->result[$i]["title"];  
            $content = $pageSupport->result[$i]["content"];  

            $part=substr($content,0,400);  
            //循環輸出每條數據  
            echo '<div class="index_side">          
                <div class="index_title">'.$title.'</div>  
                <div class="index_content">'.$part.'</div>  
                <div class="index_button">  
                   <a href="#">update</a>   <a href="#">delet</a>  
                </div>  
            </div>';  
        }  
    }  
    $pageSupport->standard_navigate(); //調用類里面的這個函數,顯示出分頁HTML  
    //關閉數據庫  
    mysql_close($con);  
 ?>  
</div>  </pre> 


來自:http://blog.csdn.net/phpfenghuo/article/details/23207099

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!