php类实现数据分页操作

来源:互联网 发布:win10系统优化固态 编辑:程序博客网 时间:2024/06/14 00:55
  1. <?php  
  2. /* 
  3.  * Created on 2013-12-3 
  4.  * 分页类 
  5.  * To change the template for this generated file go to 
  6.  * Window - Preferences - PHPeclipse - PHP - Code Templates 
  7.  */  
  8. class Pagination {  
  9.     private $each_disNums//每页显示的条目数  
  10.     private $nums//总条目数  
  11.     private $current_page//当前被选中的页  
  12.     private $sub_pages//每次显示的页数  
  13.     private $pageNums//总页数  
  14.     private $page_array = array (); //用来构造分页的数组  
  15.     private $subPage_link//每个分页的链接  
  16.     private $subPage_type//显示分页的类型  
  17.     /* 
  18.     __construct是SubPages的构造函数,用来在创建类的时候自动运行. 
  19.     @$each_disNums 每页显示的条目数 
  20.     @nums 总条目数 
  21.     @current_num 当前被选中的页 
  22.     @sub_pages 每次显示的页数 
  23.     @subPage_link 每个分页的链接 
  24.     @subPage_type 显示分页的类型 
  25.  
  26.     当@subPage_type=1的时候为普通分页模式 
  27.     example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 
  28.     当@subPage_type=2的时候为经典分页样式 
  29.     example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 
  30.     */  
  31.     function __construct($each_disNums$nums$current_page$sub_pages$subPage_link$subPage_type) {  
  32.         $this->each_disNums = intval($each_disNums);  
  33.         $this->nums = intval($nums);  
  34.         if (!$current_page) {  
  35.             $this->current_page = 1;  
  36.         } else {  
  37.             $this->current_page = intval($current_page);  
  38.         }  
  39.         $this->sub_pages = intval($sub_pages);  
  40.         $this->pageNums = ceil($nums / $each_disNums);  
  41.         $this->subPage_link = $subPage_link;  
  42.         $this->show_SubPages($subPage_type);  
  43.         //echo $this->pageNums."--".$this->sub_pages;  
  44.     }  
  45.   
  46.     /* 
  47.       __destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。 
  48.      */  
  49.     function __destruct() {  
  50.         unset ($each_disNums);  
  51.         unset ($nums);  
  52.         unset ($current_page);  
  53.         unset ($sub_pages);  
  54.         unset ($pageNums);  
  55.         unset ($page_array);  
  56.         unset ($subPage_link);  
  57.         unset ($subPage_type);  
  58.     }  
  59.   
  60.     /* 
  61.       show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页 
  62.      */  
  63.     function show_SubPages($subPage_type) {  
  64.         if ($subPage_type == 1) {  
  65.             $this->subPageCss1();  
  66.         }  
  67.         elseif ($subPage_type == 2) {  
  68.             $this->subPageCss2();  
  69.         }  
  70.     }  
  71.   
  72.     /* 
  73.       用来给建立分页的数组初始化的函数。 
  74.      */  
  75.     function initArray() {  
  76.         for ($i = 0; $i < $this->sub_pages; $i++) {  
  77.             $this->page_array[$i] = $i;  
  78.         }  
  79.         return $this->page_array;  
  80.     }  
  81.   
  82.     /* 
  83.       construct_num_Page该函数使用来构造显示的条目 
  84.       即使:[1][2][3][4][5][6][7][8][9][10] 
  85.      */  
  86.     function construct_num_Page() {  
  87.         if ($this->pageNums < $this->sub_pages) {  
  88.             $current_array = array ();  
  89.             for ($i = 0; $i < $this->pageNums; $i++) {  
  90.                 $current_array[$i] = $i +1;  
  91.             }  
  92.         } else {  
  93.             $current_array = $this->initArray();  
  94.             if ($this->current_page <= 3) {  
  95.                 for ($i = 0; $i < count($current_array); $i++) {  
  96.                     $current_array[$i] = $i +1;  
  97.                 }  
  98.             }  
  99.             elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1) {  
  100.                 for ($i = 0; $i < count($current_array); $i++) {  
  101.                     $current_array[$i] = ($this->pageNums) - ($this->sub_pages) + 1 + $i;  
  102.                 }  
  103.             } else {  
  104.                 for ($i = 0; $i < count($current_array); $i++) {  
  105.                     $current_array[$i] = $this->current_page - 2 + $i;  
  106.                 }  
  107.             }  
  108.         }  
  109.   
  110.         return $current_array;  
  111.     }  
  112.   
  113.     /* 
  114.      构造普通模式的分页 
  115.      共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页] 
  116.      */  
  117.     function subPageCss1() {  
  118.         $subPageCss1Str = "";  
  119.         $subPageCss1Str .= "共" . $this->nums . "条记录,";  
  120.         $subPageCss1Str .= "每页显示" . $this->each_disNums . "条,";  
  121.         $subPageCss1Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";  
  122.         if ($this->current_page > 1) {  
  123.             $firstPageUrl = $this->subPage_link . "1";  
  124.             $prewPageUrl = $this->subPage_link . ($this->current_page - 1);  
  125.             $subPageCss1Str .= "[<a href='$firstPageUrl'>首页</a>] ";  
  126.             $subPageCss1Str .= "[<a href='$prewPageUrl'>上一页</a>] ";  
  127.         } else {  
  128.             $subPageCss1Str .= "[首页] ";  
  129.             $subPageCss1Str .= "[上一页] ";  
  130.         }  
  131.   
  132.         if ($this->current_page < $this->pageNums) {  
  133.             $lastPageUrl = $this->subPage_link . $this->pageNums;  
  134.             $nextPageUrl = $this->subPage_link . ($this->current_page + 1);  
  135.             $subPageCss1Str .= " [<a href='$nextPageUrl'>下一页</a>] ";  
  136.             $subPageCss1Str .= "[<a href='$lastPageUrl'>尾页</a>] ";  
  137.         } else {  
  138.             $subPageCss1Str .= "[下一页] ";  
  139.             $subPageCss1Str .= "[尾页] ";  
  140.         }  
  141.   
  142.         echo $subPageCss1Str;  
  143.   
  144.     }  
  145.   
  146.     /* 
  147.      构造经典模式的分页 
  148.      当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页] 
  149.      */  
  150.     function subPageCss2() {  
  151.         $subPageCss2Str = "";  
  152.         $subPageCss2Str .= "当前第" . $this->current_page . "/" . $this->pageNums . "页 ";  
  153.   
  154.         if ($this->current_page > 1) {  
  155.             $firstPageUrl = $this->subPage_link . "1";  
  156.             $prewPageUrl = $this->subPage_link . ($this->current_page - 1);  
  157.             $subPageCss2Str .= "  <a href='$firstPageUrl'>首页</a>  ";  
  158.             $subPageCss2Str .= "  <a href='$prewPageUrl'>上一页</a>  ";  
  159.         } else {  
  160.             $subPageCss2Str .= "  首页  ";  
  161.             $subPageCss2Str .= "  上一页  ";  
  162.         }  
  163.   
  164.         $a = $this->construct_num_Page();  
  165.         for ($i = 0; $i < count($a); $i++) {  
  166.             $s = $a[$i];  
  167.             if ($s == $this->current_page) {  
  168.                 $subPageCss2Str .= "  <span style='color:red;font-weight:bold;'>" . $s . "  ";  
  169.             } else {  
  170.                 $url = $this->subPage_link . $s;  
  171.                 $subPageCss2Str .= "  <a href='$url'>" . $s . "</a>  ";  
  172.             }  
  173.         }  
  174.   
  175.         if ($this->current_page < $this->pageNums) {  
  176.             $lastPageUrl = $this->subPage_link . $this->pageNums;  
  177.             $nextPageUrl = $this->subPage_link . ($this->current_page + 1);  
  178.             $subPageCss2Str .= "  <a href='$nextPageUrl'>下一页</a>  ";  
  179.             $subPageCss2Str .= "  <a href='$lastPageUrl'>尾页</a>  ";  
  180.         } else {  
  181.             $subPageCss2Str .= "  下一页  ";  
  182.             $subPageCss2Str .= "  尾页   ";  
  183.         }  
  184.         echo $subPageCss2Str;  
  185.     }  
  186. }  
  187. ?>  

在需要分页的PHP文件中

  1. include_once ("Pagination.php");  
  2. //每页显示的条数  
  3. $pageSize = 10;  
  4. //总条目数  
  5. $pageTotal = $db->get_var("select count(*) from tb");  
  6. //每次显示的页数  
  7. $sub_pages = 10;  
  8. //得到当前是第几页  
  9. if (is_array($_GET) && count($_GET) > 0) {  
  10.     if (isset ($_GET["p"])) { //是否存在"id"的参数  
  11.         $pageCurrent = $_GET["p"];  
  12.     }  
  13. else {  
  14.     $pageCurrent = 1;  
  15. }  
  16. $goodses = $db->get_results("SELECT g.id,* FROM tb as g INNER JOIN (select id from tb  order by updatetime desc limit " . ($pageCurrent -1) * $pageSize . ",10 )as g1 ON g.id=g1.id");  

显示分页数据

  1. <?php $subPages = new Pagination($pageSize$pageTotal$pageCurrent$sub_pages"index.php?p=", 2);?>  

效果图:
原创粉丝点击