mysql 数据库 分页类

如果您加了新功能,或者是有改进,请与大家一起分享。

  1. <?php
  2.     $db_config["hostname"]    = "127.0.0.1";    //服务器地址
  3.     $db_config["username"]    = "root";        //数据库用户名
  4.     $db_config["password"]    = "root";        //数据库密码
  5.     $db_config["database"]    = "wap_blueidea_com";        //数据库名称
  6.     $db_config["charset"]    = "utf8";
  7.     $config["charset"]        = "utf-8";        //网站编码
  8.  
  9.     include('db.php');
  10.     include('pagelist.php');
  11.     $db    = new db();
  12.     $db->connect($db_config);
  13.     header("content-type:text/html;charset=".$config["charset"]);//设置页面编码
  14.     $pl = new pagelist();
  15.     $arr = $pl->get_rows('table_name');
  16.     unset($pl);
  17.     echo '<pre style="text-align:left">';
  18.     print_r($arr);
  19.     echo '</pre>';
  20.  
  21.  
  22.     //指定特殊 sql 时候
  23.     $pl = new pagelist();
  24.     $sql = 'SELECT * FROM `wap_article` AS a, `wap_article_info` AS b WHERE a.id=b.articleid';
  25.     $arr = $pl->get_rows_sql($sql);
  26.     unset($pl);
  27.     echo '<pre style="text-align:left">';
  28.     print_r($arr);
  29.     echo '</pre>';
  30. ?>

下载: pagelist.php
  1. <?php
  2. /*
  3. *    mysql数据库 分页类
  4. *   @package    pagelist
  5. *   @author        yytcpt(无影)
  6. *   @version    2008-03-27
  7. *   @copyrigth    http://www.d5s.cn/ 
  8. */
  9. /*
  10. *    分页样式
  11.     .page{float: left;font: 11px Arial, Helvetica, sans-serif; padding:6px 0; margin: 0px 10%; margin-top: 10px;}
  12.     .page a, .page strong{padding: 2px 6px; border: solid 1px #ddd;    background: #fff; text-decoration: none;}
  13.     .page a:visited{padding: 2px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
  14.     .page .break{padding: 2px 6px; border: none; background: #fff; text-decoration: none;}
  15.     .page strong{padding: 2px 6px; border-color: #999; font-weight: bold; font-size: 13px; vertical-align: top; background: #fff;}
  16.     .page a:hover{color: #fff; background: #0063DC; border-color: #036; text-decoration: none;}
  17.     .page a:hover div{color: #FFF;cursor: pointer !important;cursor: hand;}
  18. */
  19.     class pagelist{
  20.         var $page;            //分页页码
  21.         var $sql;            //分页sql
  22.         var $img_path;        //图标路径
  23.         var $img;            //图标名称
  24.         var $img_btn;        //图标地址
  25.         var $page_size;        //设置每页显示条数
  26.         var $num_btn;        //设置数字分页的显示个数
  27.         var $total_pages;    //一共分多少页
  28.         var $total_records;    //一共有多少条记录
  29.         var $url;
  30.         var $table;
  31.         var $new_sql;        //指定的SQL语句
  32.         var $db;
  33.         function __construct(){
  34.             global $db;
  35.             $this->db = $db;
  36.             $tmp_page = intval(trim($_GET["page"]));
  37.             $this->page = empty($tmp_page)?1:$tmp_page;
  38.             $this->set_table();
  39.             $this->page_size = 20;
  40.             $this->num_btn    = 9;
  41.             $this->img_path    = '/images/';
  42.             $this->img        = array("ico_first.gif", "ico_front.gif", "ico_next.gif", "ico_last.gif");
  43.         }
  44.         function set_table(){
  45.             $this->table["tablename"]    = "";
  46.             $this->table["id"]        = "id";
  47.             $this->table["orderby"]    = $this->table["id"];
  48.             $this->table["descasc"]    = "DESC";
  49.             $this->table["fileds"]    = "*";
  50.             $this->table["where"]    = "";
  51.         }
  52.         function set_img(){
  53.             $this->img_btn[0]    = "<img src='".$this->img_path.$this->img[0]."' alt='首页' border='0' align='absmiddle'/>";
  54.             $this->img_btn[1]    = "<img src='".$this->img_path.$this->img[1]."' alt='上一页' border='0' align='absmiddle'/>";
  55.             $this->img_btn[2]    = "<img src='".$this->img_path.$this->img[2]."' alt='下一页' border='0' align='absmiddle'/>";
  56.             $this->img_btn[3]    = "<img src='".$this->img_path.$this->img[3]."' alt='末页' border='0' align='absmiddle'/>";
  57.         }
  58.         function set_show_page(){
  59.             $this->set_img();        //设置翻页图片路径
  60.             $this->set_url();
  61.             $this->set_total_records();
  62.             if ($this->total_records<$this->page_size){
  63.                 $this->total_pages = 1;
  64.             }else{
  65.                 $this->total_pages = ceil($this->total_records/$this->page_size);
  66.             }
  67.             if ($this->page>$this->total_pages){
  68.                 $this->page = $this->total_pages;
  69.             }
  70.         }
  71.         function show_first_prv(){
  72.             if ($this->page==1){
  73.                 $str = "<strong>".$this->img_btn[0]."</strong> <strong>".$this->img_btn[1]."</strong>";
  74.             }else{
  75.                 $str = "<a href='".$this->url."1"."'>".$this->img_btn[0]."</a> ";    //此处1为首页,page值为1
  76.                 $str.= "<a href='".$this->url.($this->page-1)."'>".$this->img_btn[1]."</a>";
  77.             }
  78.             return $str;
  79.         }
  80.         function show_next_last(){
  81.             if ($this->page>=$this->total_pages){
  82.                 $str"<strong>".$this->img_btn[2]."</strong> <strong>".$this->img_btn[3]."</strong>";
  83.             }else{
  84.                 $str = "<a href='".$this->url.($this->page+1)."'>".$this->img_btn[2]."</a> ";
  85.                 $str.= "<a href='".$this->url.$this->total_pages."'>".$this->img_btn[3]."</a>";
  86.             }
  87.             return $str;
  88.         }
  89.         function show_num_text(){
  90.             $str = " 转到第 <input id='go_num_text' type='text' value='".$this->page."' style='border:0;border-bottom:1px solid #CCC;text-align:center;width:20px;'/> 页 ";
  91.             $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>";
  92.             return $str;
  93.         }
  94.         function show_num_select(){
  95.             if ($this->total_pages<50){
  96.                 $str = "<select onchange=\"if(this.options[this.selectedIndex].value!=''){location=this.options[this.selectedIndex].value;}\">";
  97.                 for ($i=1; $i<=$this->total_pages; $i++){
  98.                     $str.= "<option value='".$this->url.$i."' ".($this->page==$i ? " selected='selected'":"").">".$i."</option>";
  99.                 }
  100.                 $str.= "</select> ";
  101.             }else{
  102.                 $str = "";
  103.             }
  104.             return $str;
  105.         }
  106.         function show_num_btn(){
  107.             if ($this->page>=1 and $this->page<=$this->total_pages){
  108.                 $tmp_p    = ($this->num_btn-1)/2;
  109.                 if (($this->page - $tmp_p)<=0){
  110.                     $start_p    = 1;
  111.                 }else{
  112.                     if (($this->page-$tmp_p)>$this->num_btn and ($this->page-$tmp_p)>($this->total_pages - $this->num_btn+1)){
  113.                         $start_p    = $this->total_pages - $this->num_btn + 1;
  114.                     }else{
  115.                         $start_p    = $this->page - $tmp_p;
  116.                     }
  117.                 }
  118.                 if (($this->page+$tmp_p) < $this->total_pages){
  119.                     $end_p = ($this->page + $tmp_p)<$this->num_btn?$this->num_btn:($this->page + $tmp_p);
  120.                     if ($end_p>$this->total_pages){
  121.                         $end_p = $this->total_pages;
  122.                     }
  123.                 }else{
  124.                     $end_p = $this->total_pages;
  125.                 }
  126.             }
  127.             $str = "";
  128.             for ($i=$start_p; $i<=$end_p; $i++){
  129.                 if ($i==$this->page){
  130.                     $str.= " <strong>".$i."</strong> ";
  131.                 }else{
  132.                     $str.= " <a href='".$this->url.$i."'>".$i."</a> ";
  133.                 }
  134.             }
  135.             return $str;
  136.         }
  137.         function show_page_info(){
  138.             $str = "".$this->total_records."条/".$this->total_pages."";
  139.             return $str;
  140.         }
  141.         function show_page(){
  142.             if ($this->total_records<1){
  143.                 $this->set_show_page();
  144.             }
  145.             $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():"";
  146.             return $str;
  147.         }
  148.         //总页数
  149.         function set_total_pages(){
  150.             $this->total_pages = ceil($this->total_records/$this->page_size);
  151.         }
  152.         //总记录数
  153.         function set_total_records(){
  154.             if ($this->total_records==0 or !isset($this->total_records)){
  155.                 if (empty($this->count_sql) and !empty($this->table["tablename"])){
  156.                     $sql = "SELECT count(".$this->table["id"].") as count_id FROM `".$this->table["tablename"]."` ".($this->table["where"]!=""?" WHERE ".$this->table["where"]:"");
  157.                 }else{
  158.                     $sql = preg_replace("/SELECT(.*?)FROM(.*?)/i", "SELECT count(id) AS count_id FROM\\2", $this->sql);
  159.                 }
  160.                 $arr = $this->db->row_query_one($sql);
  161.                 $this->total_records = $arr["count_id"];
  162.             }
  163.         }
  164.         /*
  165.          * 根据sql返回查询数据
  166.          * 指定$sql时,不必指定limit
  167.          */
  168.         function get_rows_by_sql($sql){
  169.             $this->sql = $sql." LIMIT ".$this->page_size*($this->page-1).", ".$this->page_size;    //指定的SQL;
  170.             return $this->db->row_query($this->sql);
  171.         }
  172.         /*
  173.          * 最常用的分页方法,只需要传3个参数
  174.          * $tablename 表名, $where 查询条件, $orderby 排序字段(默认以id倒序排列)
  175.          */
  176.         function get_rows($tablename, $where="", $orderby=""){
  177.             $this->table["tablename"]    = $tablename;
  178.             $this->table["where"]        = $where;
  179.             $orderby ? $this->table["orderby"] = $orderby : "";
  180.             $arr = array(
  181.                 "page"    => $this->show_page(),            //分页代码
  182.                 "rows"    => $this->get_rows_by_sql(),    //记录数
  183.                 "sum"    => $this->total_records,        //总记录数
  184.             );
  185.             return $arr;
  186.         }
  187.         /*
  188.          * 特殊查询,$sql_query 查询sql语句, $row_count 统计总数
  189.          */
  190.         function get_rows_sql($sql_query, $row_count=0) {
  191.             $this->total_records = $row_count;
  192.             $arr["rows"]    = $this->get_rows_by_sql($sql_query);
  193.             $arr["page"]    = $this->show_page();
  194.             $arr["sum"]        = $this->total_records;
  195.             return $arr;
  196.         }
  197.         function get_sql(){
  198.             if ($this->total_records>10000) {
  199.                 $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;
  200.             }else{
  201.                 $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;
  202.             }
  203.             //SELECT * FROM articles ORDER BY id DESC LIMIT 0, 20
  204.             //SELECT * FROM articles WHERE category_id = 123 AND id >= (SELECT id FROM articles ORDER BY id LIMIT 10000, 1) LIMIT 10
  205.             return $this->sql;        //SQL语句
  206.         }
  207.         function set_url(){
  208.             $arr_url = array();
  209.             parse_str($_SERVER["QUERY_STRING"], $arr_url);
  210.             unset($arr_url["page"]);
  211.             if (empty($arr_url)){
  212.                 $str = "page=";
  213.             }else{
  214.                 $str = http_build_query($arr_url)."&page=";
  215.             }
  216.             $this->url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str;
  217.         }
  218.     }
  219. ?>
此条目发表在 程序 分类目录。将固定链接加入收藏夹。

mysql 数据库 分页类》有 6 条评论

  1. 说:

    if ($this->total_records>10000) {

    假设,我数据量超过1万条,而且根据修改时间来倒序。

    也就是说 ID为 100的那条 有可能比 ID为 10001 的那条来的 新。

    此时,你的分页将无效。

    SELECT * FROM ‘tb_list` WHERE id>=(SELECT id FROM `tb_list` ORDER BY id LIMIT 30, 1) ORDER BY edittime desc LIMIT 5

    其中“SELECT id FROM `tb_list` ORDER BY id LIMIT 30, 1” 出来的ID 都在1万以上了, 那么 ID=100 的那条最新修改的 就出不来了。

    -。-

    • owen 说:

      对于这种不以id为顺序的排序,或者是那种有些特殊的查询,应该自己单独写sql语句,而不应该用这个通用方法。

  2. xmc404 说:

    请问怎么输出呢?不要用priint_r

  3. xmc404 说:

    方便加个QQ吗?我的是14143485

  4. xmc404 说:

    分页显示是解决了,但到了最后一页时 分页部分就没有了

  5. regfora 说:

    请问分页到了最后一页,分页显示的那排就不见了,是什么原因?

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>