php mysql数据库操作分页类


Posted in PHP onJune 04, 2008
<?php 
/* 
 *    mysql数据库 分页类 
 *    @package    pagelist 
 *    @author        yytcpt(无影) 
 *    @version    2008-03-27 
 *    @copyrigth    http://www.d5s.cn/  
 */ 
/* 
 *    分页样式 
    .page{float: left;font: 11px Arial, Helvetica, sans-serif; padding:6px 0; margin: 0px 10%; margin-top: 10px;} 
    .page a, .page strong{padding: 2px 6px; border: solid 1px #ddd;    background: #fff; text-decoration: none;} 
    .page a:visited{padding: 2px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;} 
    .page .break{padding: 2px 6px; border: none; background: #fff; text-decoration: none;} 
    .page strong{padding: 2px 6px; border-color: #999; font-weight: bold; font-size: 13px; vertical-align: top; background: #fff;} 
    .page a:hover{color: #fff; background: #0063DC; border-color: #036; text-decoration: none;} 
    .page a:hover div{color: #FFF;cursor: pointer !important;cursor: hand;} 
*/ 
    class pagelist{ 
        var $page;            //分页页码 
        var $sql;            //分页sql 
        var $img_path;        //图标路径 
        var $img;            //图标名称 
        var $img_btn;        //图标地址 
        var $page_size;        //设置每页显示条数 
        var $num_btn;        //设置数字分页的显示个数 
        var $total_pages;    //一共分多少页 
        var $total_records;    //一共有多少条记录 
        var $url; 
        var $table; 
        var $new_sql;        //指定的SQL语句 
        var $db; 
        function __construct(){ 
            global $db; 
            $this->db = $db; 
            $tmp_page = intval(trim($_GET["page"])); 
            $this->page = empty($tmp_page)?1:$tmp_page; 
            $this->set_table(); 
            $this->page_size = 20; 
            $this->num_btn    = 9; 
            $this->img_path    = '/images/'; 
            $this->img        = array("ico_first.gif", "ico_front.gif", "ico_next.gif", "ico_last.gif"); 
        } 
        function set_table(){ 
            $this->table["tablename"]    = ""; 
            $this->table["id"]        = "id"; 
            $this->table["orderby"]    = $this->table["id"]; 
            $this->table["descasc"]    = "DESC"; 
            $this->table["fileds"]    = "*"; 
            $this->table["where"]    = ""; 
        } 
        function set_img(){ 
            $this->img_btn[0]    = "<img src='".$this->img_path.$this->img[0]."' alt='首页' border='0' align='absmiddle'/>"; 
            $this->img_btn[1]    = "<img src='".$this->img_path.$this->img[1]."' alt='上一页' border='0' align='absmiddle'/>"; 
            $this->img_btn[2]    = "<img src='".$this->img_path.$this->img[2]."' alt='下一页' border='0' align='absmiddle'/>"; 
            $this->img_btn[3]    = "<img src='".$this->img_path.$this->img[3]."' alt='末页' border='0' align='absmiddle'/>"; 
        } 
        function set_show_page(){ 
            $this->set_img();        //设置翻页图片路径 
            $this->set_url(); 
            $this->set_total_records(); 
            if ($this->total_records<$this->page_size){ 
                $this->total_pages = 1; 
            }else{ 
                $this->total_pages = ceil($this->total_records/$this->page_size); 
            } 
            if ($this->page>$this->total_pages){ 
                $this->page = $this->total_pages; 
            } 
        } 
        function show_first_prv(){ 
            if ($this->page==1){ 
                $str = "<strong>".$this->img_btn[0]."</strong> <strong>".$this->img_btn[1]."</strong>"; 
            }else{ 
                $str = "<a href='".$this->url."1"."'>".$this->img_btn[0]."</a> ";    //此处1为首页,page值为1 
                $str.= "<a href='".$this->url.($this->page-1)."'>".$this->img_btn[1]."</a>"; 
            } 
            return $str; 
        } 
        function show_next_last(){ 
            if ($this->page>=$this->total_pages){ 
                $str =  "<strong>".$this->img_btn[2]."</strong> <strong>".$this->img_btn[3]."</strong>"; 
            }else{ 
                $str = "<a href='".$this->url.($this->page+1)."'>".$this->img_btn[2]."</a> "; 
                $str.= "<a href='".$this->url.$this->total_pages."'>".$this->img_btn[3]."</a>"; 
            } 
            return $str; 
        } 
        function show_num_text(){ 
            $str = " 转到第 <input id='go_num_text' type='text' value='".$this->page."' style='border:0;border-bottom:1px solid #CCC;text-align:center;width:20px;'/> 页 "; 
            $str.= "<a href='#' onClick=\"window.location='".$this->url."'+document.getElementById('go_num_text').value;\" style='font-family: Arial, Helvetica, sans-serif;font-weight:bold;font-size:14px;'>[Go]</a>"; 
            return $str; 
        } 
        function show_num_select(){ 
            if ($this->total_pages<50){ 
                $str = "<select onchange=\"if(this.options[this.selectedIndex].value!=''){location=this.options[this.selectedIndex].value;}\">"; 
                for ($i=1; $i<=$this->total_pages; $i++){ 
                    $str.= "<option value='".$this->url.$i."' ".($this->page==$i ? " selected='selected'":"").">".$i."</option>"; 
                } 
                $str.= "</select> "; 
            }else{ 
                $str = ""; 
            } 
            return $str; 
        } 
        function show_num_btn(){ 
            if ($this->page>=1 and $this->page<=$this->total_pages){ 
                $tmp_p    = ($this->num_btn-1)/2; 
                if (($this->page - $tmp_p)<=0){ 
                    $start_p    = 1; 
                }else{ 
                    if (($this->page-$tmp_p)>$this->num_btn and ($this->page-$tmp_p)>($this->total_pages - $this->num_btn+1)){ 
                        $start_p    = $this->total_pages - $this->num_btn + 1; 
                    }else{ 
                        $start_p    = $this->page - $tmp_p; 
                    } 
                } 
                if (($this->page+$tmp_p) < $this->total_pages){ 
                    $end_p = ($this->page + $tmp_p)<$this->num_btn?$this->num_btn:($this->page + $tmp_p); 
                    if ($end_p>$this->total_pages){ 
                        $end_p = $this->total_pages; 
                    } 
                }else{ 
                    $end_p = $this->total_pages; 
                } 
            } 
            $str = ""; 
            for ($i=$start_p; $i<=$end_p; $i++){ 
                if ($i==$this->page){ 
                    $str.= " <strong>".$i."</strong> "; 
                }else{ 
                    $str.= " <a href='".$this->url.$i."'>".$i."</a> "; 
                } 
            } 
            return $str; 
        } 
        function show_page_info(){ 
            $str = " 共".$this->total_records."条/".$this->total_pages."页"; 
            return $str; 
        } 
        function show_page(){ 
            if ($this->total_records<1){ 
                $this->set_show_page(); 
            } 
            $str = $this->total_pages>1 ? $this->show_first_prv().$this->show_num_btn().$this->show_next_last().$this->show_page_info().$this->show_num_text():""; 
            return $str; 
        } 
        //总页数 
        function set_total_pages(){ 
            $this->total_pages = ceil($this->total_records/$this->page_size); 
        } 
        //总记录数 
        function set_total_records(){ 
            if ($this->total_records==0 or !isset($this->total_records)){ 
                if (empty($this->count_sql) and !empty($this->table["tablename"])){ 
                    $sql = "SELECT count(".$this->table["id"].") as count_id FROM `".$this->table["tablename"]."` ".($this->table["where"]!=""?" WHERE ".$this->table["where"]:""); 
                }else{ 
                    $sql = preg_replace("/SELECT(.*?)FROM(.*?)/i", "SELECT count(id) AS count_id FROM\\2", $this->sql); 
                } 
                $arr = $this->db->row_query_one($sql); 
                $this->total_records = $arr["count_id"]; 
            } 
        } 
        /* 
         * 根据sql返回查询数据 
         * 指定$sql时,不必指定limit 
         */ 
        function get_rows_by_sql($sql){ 
            $this->sql = $sql." LIMIT ".$this->page_size*($this->page-1).", ".$this->page_size;    //指定的SQL; 
            return $this->db->row_query($this->sql); 
        } 
        /* 
         * 最常用的分页方法,只需要传3个参数 
         * $tablename 表名, $where 查询条件, $orderby 排序字段(默认以id倒序排列) 
         */ 
        function get_rows($tablename, $where="", $orderby=""){ 
            $this->table["tablename"]    = $tablename; 
            $this->table["where"]        = $where; 
            $orderby ? $this->table["orderby"] = $orderby : ""; 
            $arr = array( 
                "page"    => $this->show_page(),            //分页代码 
                "rows"    => $this->get_rows_by_sql(),    //记录数 
                "sum"    => $this->total_records,        //总记录数 
            ); 
            return $arr; 
        } 
        /* 
         * 特殊查询,$sql_query 查询sql语句, $row_count 统计总数 
         */ 
        function get_rows_sql($sql_query, $row_count=0) { 
            $this->total_records = $row_count; 
            $arr["rows"]    = $this->get_rows_by_sql($sql_query); 
            $arr["page"]    = $this->show_page(); 
            $arr["sum"]        = $this->total_records; 
            return $arr; 
        } 
        function get_sql(){ 
            if ($this->total_records>10000) { 
                $this->sql = "SELECT ".$this->table["fileds"]." FROM `".$this->table["tablename"]."` ".($this->table["where"]!=""?" WHERE ".$this->table["where"].' AND '.$this->table["id"].'>=':' WHERE '.$this->table["id"].'>=').'(SELECT '.$this->table["id"].' FROM `'.$this->table["tablename"].'` ORDER BY '.$this->table["id"].' LIMIT '.$this->page_size*($this->page-1).', 1)'." ORDER BY ".$this->table["orderby"]." ".$this->table["descasc"]." LIMIT ".$this->page_size; 
            }else{ 
                $this->sql = "SELECT ".$this->table["fileds"]." FROM `".$this->table["tablename"]."` ".($this->table["where"]!=""?" WHERE ".$this->table["where"]:"")." ORDER BY ".$this->table["orderby"]." ".$this->table["descasc"]." LIMIT ".$this->page_size*($this->page-1).", ".$this->page_size; 
            } 
            //SELECT * FROM articles ORDER BY id DESC LIMIT 0, 20 
            //SELECT * FROM articles WHERE category_id = 123 AND id >= (SELECT id FROM articles ORDER BY id LIMIT 10000, 1) LIMIT 10 
            return $this->sql;        //SQL语句 
        } 
        function set_url(){ 
            $arr_url = array(); 
            parse_str($_SERVER["QUERY_STRING"], $arr_url); 
            unset($arr_url["page"]); 
            if (empty($arr_url)){ 
                $str = "page="; 
            }else{ 
                $str = http_build_query($arr_url)."&page="; 
            } 
            $this->url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str; 
        } 
    } 
?>

<?php  
    $db_config["hostname"]    = "127.0.0.1";    //服务器地址  
    $db_config["username"]    = "root";        //数据库用户名  
    $db_config["password"]    = "root";        //数据库密码  
    $db_config["database"]    = "wap_blueidea_com";        //数据库名称  
    $db_config["charset"]    = "utf8";  
    $config["charset"]        = "utf-8";        //网站编码      include('db.php');  
    include('pagelist.php');  
    $db    = new db();  
    $db->connect($db_config);  
    header("content-type:text/html;charset=".$config["charset"]);//设置页面编码  
    $pl = new pagelist();  
    $arr = $pl->get_rows('table_name');  
    unset($pl);  
    echo '<pre style="text-align:left">';  
    print_r($arr);  
    echo '</pre>';  
    //指定特殊 sql 时候  
    $pl = new pagelist();  
    $sql = 'SELECT * FROM `wap_article` AS a, `wap_article_info` AS b WHERE a.id=b.articleid';  
    $arr = $pl->get_rows_sql($sql);  
    unset($pl);  
    echo '<pre style="text-align:left">';  
    print_r($arr);  
    echo '</pre>';  
?> 
当表中的记录总数在 10000条以上时,使用了 子查询分页,这样效率会更高一些,数据量小的时候,直接查询更快。
PHP 相关文章推荐
用PHP实现多服务器共享SESSION数据的方法
Mar 16 PHP
PHP 地址栏信息的获取代码
Jan 07 PHP
PHP iconv 函数转gb2312的bug解决方法
Oct 11 PHP
PHP运行模式的深入理解
Jun 03 PHP
基于Discuz security.inc.php代码的深入分析
Jun 03 PHP
php中apc缓存使用示例
Dec 25 PHP
Yii查询生成器(Query Builder)用法实例教程
Sep 04 PHP
PHP动态编译出现Cannot find autoconf的解决方法
Nov 05 PHP
PHP中使用php5-ffmpeg撷取视频图片实例
Jan 07 PHP
php从数据库查询结果生成树形列表的方法
Apr 17 PHP
php有效防止图片盗用、盗链的两种方法
Nov 01 PHP
PHP实现动态删除XML数据的方法示例
Mar 30 PHP
php mysql数据库操作类
Jun 04 #PHP
CodeIgniter php mvc框架 中国网站
May 26 #PHP
五个PHP程序员工具
May 26 #PHP
需要使用php模板的朋友必看的很多个顶级PHP模板引擎比较分析
May 26 #PHP
JS实现php的伪分页
May 25 #PHP
php中iconv函数使用方法
May 24 #PHP
在PHP中使用模板的方法
May 24 #PHP
You might like
全国FM电台频率大全 - 30 宁夏回族自治区
2020/03/11 无线电
php cookies中删除的一般赋值方法
2011/05/07 PHP
PHP+jquery+ajax实现即时聊天功能实例
2014/12/23 PHP
yii2实现分页,带搜索的分页功能示例
2017/01/07 PHP
php 删除指定文件夹的实例讲解
2017/07/25 PHP
js中document.getElementByid、document.all和document.layers区分介绍
2011/12/08 Javascript
js实现广告漂浮效果的小例子
2013/07/02 Javascript
类似天猫商品详情随浏览器移动的示例代码
2014/02/27 Javascript
js控制文本框只输入数字和小数点的方法
2015/03/10 Javascript
jQuery对指定元素中指定字符串进行替换的方法
2015/03/17 Javascript
详解jQuery移动页面开发中的ui-grid网格布局使用
2015/12/03 Javascript
js判断某个字符出现的次数的简单实例
2016/06/03 Javascript
js将table的每个td的内容自动赋值给其title属性的方法
2016/10/13 Javascript
利用jquery实现实时更新歌词的方法
2017/01/06 Javascript
详解nodejs微信公众号开发——2.自动回复
2017/04/10 NodeJs
javascript浏览器用户代理检测脚本实现方法
2017/10/27 Javascript
vue 2.0 购物车小球抛物线的示例代码
2018/02/01 Javascript
vue实现压缩图片预览并上传功能(promise封装)
2019/01/10 Javascript
vue-cli系列之vue-cli-service整体架构浅析
2019/01/14 Javascript
详解Vue的watch中的immediate与watch是什么意思
2019/12/30 Javascript
Vue按时间段查询数据组件使用详解
2020/08/21 Javascript
vue实现验证用户名是否可用
2021/01/20 Vue.js
python实现批量监控网站
2016/09/09 Python
用python结合jieba和wordcloud实现词云效果
2017/09/05 Python
python 3.5实现检测路由器流量并写入txt的方法实例
2017/12/17 Python
python中使用%与.format格式化文本方法解析
2017/12/27 Python
Python魔法方法详解
2019/02/13 Python
python实现二分类的卡方分箱示例
2019/11/22 Python
Python 获取numpy.array索引值的实例
2019/12/06 Python
Python基于read(size)方法读取超大文件
2020/03/12 Python
amazeui时间组件的实现示例
2020/08/18 HTML / CSS
美丽的现代设计家具:2Modern
2018/07/26 全球购物
Notino罗马尼亚网站:购买香水和化妆品
2019/07/20 全球购物
易程科技软件测试笔试
2013/03/24 面试题
乡镇干部十八大感言
2014/02/17 职场文书
人力资源管理毕业求职信
2014/08/05 职场文书