PHP 分页类

来源:互联网 发布:老电视能看网络电视吗 编辑:程序博客网 时间:2024/05/16 14:13
<?php
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 "<tfoot><tr><td><div class='pagination'>";
    echo "<form action=".$_SERVER['PHP_SELF']." method=\"get\">";
    
    echo "<font color = red size ='4'>2222222222第".$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></td></tr></tfoot>";


 } 

?>

原创粉丝点击