PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » PHP中高级 » pdo分页类1.0
本页主题: pdo分页类1.0 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

自由的龙

该用户目前不在线
级别: 中级程序员
精华: 1
发帖: 1264
威望: 1267 点
金钱: 12660 PYMB
贡献值: 0 点
在线时间:0(小时)
注册时间:2006-04-16
最后登录:2006-06-27

pdo分页类1.0


Copy code
<?php
class pdo_page{
/*变量定义*/
    /*pdo数据源*/
    var $db_driver='';
    var $db_host='';
    var $db_user='';
    var $db_password='';
    var $db_char='';
    var $db_name='';
    var $db_table='';
    var $db_table_field='';
    var $db='';   //数据库连接句柄
    /*分页显示参数设置*/
    var $page_size=0; //每页显示的记录数目
    var $link_num=0;   //显示页码链接的数目

    var $page=1; //页码
    var $records=0; //表中记录总数
    var $page_count=0; //总页数
    var $pagestring='';   //前后分页链接字符串
    var $page_link='';   //页码链接字符串
    var $page_select='';   //表单跳转页字符串
    var $page_jump='';   //text筐输入页码跳转
/*函数定义*/
    /*连接数据库*/
    function db_conn(){

      try{
        $this->db=new pdo(
                "$this->db_driver:dbname=$this->db_name;host=$this->db_host;charset=$this->db_char",
                "$this->db_user",
                "$this->db_password"
              );
        return $this->db;
      }
      catch(pdoexception $e)
      {
        die($e->getmessage());
      }
    }

    /*页码处理*/
    function set_page(){
      if(isset($_REQUEST['page'])){
        $this->page=intval($_REQUEST['page']);
      }
      else{
        $this->page=1;
      }
    }

    /*获取db中记录的数目*/
    function get_records(){
      $sql="select count(*) from $this->db_table";
      $stmt=$this->db->prepare($sql);
      $stmt->execute();
      while($f=$stmt->fetch()){
          $this->records=$f[0];
      }
    }

    function page_link(){
    /*翻页链接 begin*/
    /*前后页链接字符串 begin*/
    if($this->page==1){
        $this->pagestring.='第一页|上一页';       //如果是首页,无链接
    }
    else{
        $this->pagestring.='<a href=?page=1>第一页</a>|<a href=?page='.($this->page-1).'>上一页</a>'; //不为首页,有链接
    }
    if($this->page==$this->page_count||$this->page_count==0){
        $this->pagestring.='下一页|尾页';       //如果是最后一页,无链接
    }
    else{
        $this->pagestring.='<a href=?page='.($this->page+1).'>下一页</a>|<a href=?page='.$this->page_count.'>尾页</a>'; //不是最后一页,有链接
}
    /*前后页链接字符传字符串 end*/

    /*页码链接字符串 begin*/
    for($i=$this->page;$i<=$this->page+$this->link_num-1;$i++){
        if($i<=$this->page_count){
          $this->page_link.='<a href=?page='.$i.'>['.$i.']</a> ';
          $last_page=$i;
        }
    }
    if($i-$this->link_num-1<1){
      $front_page=1;
    }
    else{
      $front_page=$i-$this->link_num-1;
    }
    if($last_page==$this->page_count){
      $back_page=$last_page;
    }
    else{
      $back_page=$last_page+1;
    }
    $this->page_link='<a href=?page='.$front_page.'><<</a>'.' '.$this->page_link.' '.'<a href=?page='.$back_page.'>>></a>';
    /*页码链接字符串 end*/

    /*select页码 begin*/
    $this->page_select="<form action='' method=post><select name=page>";
    for($i=1;$i<=$this->page_count;$i++){
        if($i==$this->page){
          $this->page_select.="<option selected>$i</option>";
        }
        else{
          $this->page_select.="<option>$i</option>";
        }
    }
    $this->page_select.="</select><input type=submit value=go></form>";
    /*select页码 end*/

    /*input跳转表单begin*/
    $this->page_jump="<form action='' method=post><input type=text size=1 name=page value=$this->page><input type=submit value=go>";
    /*input跳转表单end*/

    /*翻页链接 end*/
    }

    /*获取数据 begin*/
    function fetch_data(){
      if($this->records){
        $sql="select * from $this->db_table limit ".($this->page-1)*$this->page_size.",$this->page_size";
        $stmt=$this->db->prepare($sql);
        $stmt->execute();
        echo "<center><table border=1 width=60%><tr>";
        /*取字段名称 begin*/
        $field_count=count($this->db_table_field);
        for($i=0;$i<$field_count;$i++){
          $field_name=$this->db_table_field[$i];
          echo "<td><center><b>$field_name</b></center></td>";
        }
        echo "</tr>";
        /*取字段名称 end*/
        /*获取数据begin*/
        while($f=$stmt->fetch()){
          echo "<tr>";
          for($i=0;$i<$field_count;$i++){
              $field_name=$this->db_table_field[$i];
              $field_value=$f["$field_name"];
              echo "<td><center>$field_value</center></td>";
          }
          echo "</tr>";
        }
        /*获取数据end*/
        echo "</table></center>";
      }
    }

    /*获取数据 end*/

    /*建立分页begin*/
    function create_page(){
      $this->db_conn();
      $this->set_page();
      $this->get_records();
      $this->page_count= ceil($this->records/$this->page_size);
      $this->page_link();
      $this->fetch_data();
    }
    /*建立分页end*/
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////example///////////////////////////
$page=new pdo_page;
/*db参数设置begin*/
  $page->db_driver='mysql';                 //db驱动
  $page->db_host='localhost';                 //dbms地址
  $page->db_user='root';                   //dbms帐户
  $page->db_password='goldfish';               //dbms密码
  $page->db_name='goldfish';                 //db名称
  $page->db_table='goldfish';                 //表名
  $page->db_table_field=array('id','name','age');   //字段数组,将要显示的字段名称写入该数组
/*db参数设置end*/

/*分页参数设置begin*/
  $page->page_size=5;                     //每页显示记录的数目
  $page->link_num=6;                       //显示翻页链接的数目
  $page->create_page();                     //生成分页
/*分页参数设置end*/

/*翻页链接显示输出begin*/
  echo '<center>共有'.$page->records.'条记录';     //表中记录的总数
  echo '   ';
  echo '第'.$page->page.'页/';
  echo '共'.$page->page_count.'页</center>';       //总页数

  echo '<center>'.$page->pagestring.'</center>';   //'首页'、'上一页'、'下一页'、'尾页'--链接样式
  echo '<center>'.$page->page_link.'</center>';     //[1]、[2]、[3]--链接样式
  echo '<center>'.$page->page_select.'</center>';   //表单翻页样式
  echo '<center>'.$page->page_jump.'</center>';
/*翻页链接显示输出end*/
?>
顶端 Posted: 2006-04-25 13:43 | [楼 主]
PHP学会网 php培训网 PHP暑期培训 PHP寒假培训 PHP假期培训 » PHP中高级

时:11-23 20:15 Copyright © 2006 phpwhy.com 权
ICP05060669

曳息 -