首 页IT知识库翔宇问吧收藏本站
当前位置:翔宇亭IT乐园IT知识库WEBPHP

php pdo分页同时支持mysql和access

减小字体 增大字体 作者:baoxuehao.cn  来源:asp,php,seo知识积累  发布时间:2010-08-03 10:49:00

access是不支持limit分页的,想同的关键,问题马上就解决了。

看注释应该很容易理解,access的分页sql从asp json 的google code上来的 

<?php
/**
 //分页类
 */
class Page{

 //字段属性
 public $param; //分页参数

 public $pagesize; //每页大小

 public $sql;//当数据库为access,无效

 public $conn;

 //设置数据库类型
 public $dbtype;

 //当数据库为access配置项
 public $tbname;//表名

 public $prikey;//主键

 //public $url;

 public function __set($name,$value){
  $this->$name = $value;
 }
 //初始化
 public function __construct(){

  //$this->conn=new mysqli("localhost","root","147258","empirecms");
  //$this->conn->set_charset('utf8');

 }

 //销毁
 public function __destruct(){
 }

 //开始
 public function getStart(){

  //$param=$this->param;

  $param=(isset($_GET[$this->param])) ? $_GET[$this->param] : 1;

  return ($param-1) * $this->pagesize;
 }
 //生成SQL语句
 //mysql
 public function getMysqlSql(){
  return $this->sql . " limit " . $this->getStart() ."," .$this->pagesize . "";
  //return "test";
 }
 //access的查询语句,参考json asp google
 public function getAccessSql(){
  $end_n=$this->getStart() + $this->pagesize;

  if($end_n>$this->pagesize){
   $accsql = "SELECT TOP ". $this->pagesize ." * FROM [" . $this->tbname . "] as a where Not Exists(Select * From (Select Top ". $this->getStart() ." * From [" . $this->tbname . "] order by [" . $this->prikey . "] DESC) b Where b.[" . $this->prikey . "]=a.[" . $this->prikey . "] ) ORDER BY a.[" . $this->prikey . "] DESC" ;
  }else{
   $accsql = "SELECT TOP ". $this->pagesize ." * FROM [" . $this->tbname . "] ORDER BY [" . $this->prikey . "] DESC" ;
  }

  return $accsql;

 }

 //获取记录
 public function getRecordSet(){
  switch($this->dbtype){
   case "ACCESS":
    $ssql=$this->getAccessSql();
    break;
   case "MYSQL":
    $ssql=$this->getMysqlSql();
    break;
  }

  $rs1=$this->conn->query($ssql);

  return $rs1;
 }
 //获取总页数
 public function getPageCount(){

  return $this->getRecordCount() / $this->pagesize;

 }
 //获取总记录数
 public function getRecordCount(){
  $rs=$this->conn->query("select count(*) from [" .$this->tbname ."]");
  $row=$rs->fetchColumn();
  return $row;
 }

 //获取分页导航
 public function getPageTool(){

  $pagecount= round($this->getPageCount());

  $param=(isset($_GET[$this->param])) ? $_GET[$this->param] : 1;

  if($param<=1){
   echo "首页 上一页 ";
  }else{
   echo "<a href='?".$this->param."=1'>首页</a> <a href='?".$this->param."=".($param-1)."'>上一页</A> ";
  }
  if($param>=$pagecount){
   echo "下一页 尾页";
  }else{
   echo "<A href='?".$this->param."=".($param+1)."'>下一页</A> <A href='?".$this->param."=".$pagecount."'>尾页</A>";
  }
 }
}
?>

本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

知识评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 1分

内 容:

            请注意用语文明且合法,不要发布带有攻击性、侮辱性的言论,谢谢合作!

         通知管理员 验证码:

关于本站 | 网站帮助 | 广告合作 | 网站声明 | 友情连接 | 网站地图 | 用户守则 | 联系我们 |
本站大多数内容来自互联网或网站会员发布,如有侵权,请来信告之,谢谢!
Copyright © 2007-2017 biye5u.com. All Rights Reserved.
网站备案号:黑ICP备13005378号-3