[PHP] PHP分页类,有四种样式共选择 →→→→→进入此内容的聊天室

来自 , 2019-12-24, 写在 PHP, 查看 146 次.
URL http://www.code666.cn/view/465636eb
  1. 查询时用到的:
  2. $pagesize=20; //每页显示数量
  3. if($_POST["pageid"]){
  4.         if($_POST["form1"]){
  5.                 $pageval=1;
  6.         }else{
  7.                 $pageval=$_POST["pageid"];
  8.         }
  9.         $pageid=($pageval-1)*$pagesize;
  10.         $pageid.=',';
  11. }
  12. if($_GET["p"]){
  13.         $pageval=$_GET["p"];
  14.         $pageid=($pageval-1)*$pagesize;
  15.         $pageid.=',';
  16. }
  17. sql:
  18. $sql="select * from table order by addtime desc limit $pageid $pagesize";
  19.  
  20. 使用方法:
  21. <?php
  22. //分页类调用
  23. require("inc/page.class.php");
  24. $params = array(
  25.                         'total_rows'=>$Num, //信息总数
  26.                         'method'=>'get', //方法
  27.                         'ajax_func_name'=>'goToPage',
  28.                         'now_page'=>$pageval, //当前页
  29.                         'list_rows'=>$pagesize, //分页显示条数
  30. );
  31. $page = new Core_Lib_Page($params);
  32. echo $page->show(2);
  33. ?>
  34.  
  35. page.class.php源代码:(打包要积分下载)
  36. <?php
  37. /**
  38.  * PHP分页类
  39.  * 有四种分页样式:可调用show(1);show(2);show(3);show(4);查看
  40.  * 分页样式可灵活使用自定义多种样式
  41. */
  42. class Core_Lib_Page
  43. {
  44.         public     $first_row;            //起始行数
  45.         public     $list_rows;            //列表每页显示行数
  46.         protected  $total_pages;          //总页数
  47.         protected  $total_rows;           //总行数
  48.         protected  $now_page;         //当前页数
  49.         protected  $method  = 'defalut'; //处理情况 ajax分页 html分页(静态化时) 普通get方式
  50.         protected  $parameter = '';
  51.         protected  $page_name;        //分页参数的名称
  52.         protected  $ajax_func_name;   //ajax分页下的方法名称
  53.         public     $plus = 3;         //分页偏移量
  54.         protected  $url;
  55.        
  56.         /**
  57.          * 构造函数
  58.          * @param unknown_type $data
  59.          */
  60.         public function __construct($data = array())
  61.         {
  62.                 $this->total_rows = $data['total_rows'];
  63.  
  64.                 $this->parameter = !empty($data['parameter']) ? $data['parameter'] : '';
  65.                 $this->list_rows = !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
  66.                 $this->total_pages = ceil($this->total_rows / $this->list_rows);
  67.                 $this->page_name = !empty($data['page_name']) ? $data['page_name'] : 'p';
  68.                 $this->ajax_func_name = !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : '';
  69.                 $this->getsession = $data['getsession'];
  70.                 $this->method = !empty($data['method']) ? $data['method'] : '';
  71.                
  72.                
  73.                 /* 当前页面 */
  74.                 if(!empty($data['now_page']))
  75.                 {
  76.                         $this->now_page = intval($data['now_page']);
  77.                 }else{
  78.                         $this->now_page = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;
  79.                 }
  80.                 $this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;
  81.        
  82.                
  83.                 if(!empty($this->total_pages) && $this->now_page > $this->total_pages)
  84.                 {
  85.                         $this->now_page = $this->total_pages;
  86.                 }
  87.                 $this->first_row = $this->list_rows * ($this->now_page - 1);
  88.         }      
  89.        
  90.         /**
  91.          * 得到当前连接
  92.          * @param $page
  93.          * @param $text
  94.          * @return string
  95.          */
  96.         protected function _get_link($page,$text)
  97.         {
  98.                 switch ($this->method) {
  99.                         case 'ajax':
  100.                                 $parameter = '';
  101.                                 if($this->parameter)
  102.                                 {
  103.                                         $parameter = ','.$this->parameter;
  104.                                 }
  105.                                 return '<a onclick="' . $this->ajax_func_name . '(\'' . $page . '\''.$parameter.')" href="javascript:void(0)">' . $text . '</a>' . "\n";
  106.                         break;
  107.                        
  108.                         case 'html':
  109.                                 $url = str_replace('?', $page,$this->parameter);
  110.                                 return '<a href="' .$url . '">' . $text . '</a>' . "\n";
  111.                         break;
  112.                        
  113.                         default:
  114.                                 return '<a href="' . $this->_get_url($page) . $this->getsession . '">' . $text . '</a>' . "\n";
  115.                         break;
  116.                 }
  117.         }
  118.        
  119.    
  120.     /**
  121.      * 设置当前页面链接
  122.      */
  123.     protected function _set_url()
  124.     {
  125.         $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
  126.         $parse = parse_url($url);
  127.         if(isset($parse['query'])) {
  128.             parse_str($parse['query'],$params);
  129.             unset($params[$this->page_name]);
  130.             $url   =  $parse['path'].'?'.http_build_query($params);
  131.         }
  132.         if(!empty($params))
  133.         {
  134.                 $url .= '&';
  135.         }
  136.         $this->url = $url;
  137.     }
  138.        
  139.     /**
  140.      * 得到$page的url
  141.      * @param $page 页面
  142.      * @return string
  143.      */
  144.     protected function _get_url($page)
  145.     {
  146.         if($this->url === NULL)
  147.         {
  148.                 $this->_set_url();     
  149.         }
  150.         //      $lable = strpos('&', $this->url) === FALSE ? '' : '&';
  151.         return $this->url . $this->page_name . '=' . $page;
  152.     }
  153.    
  154.    
  155.     /**
  156.      * 得到第一页
  157.      * @return string
  158.      */
  159.     public function first_page($name = '第一页')
  160.     {
  161.                 if($this->now_page > 5)
  162.                 {
  163.                         return $this->_get_link('1', $name);
  164.                 }      
  165.                 return '';
  166.     }
  167.    
  168.     /**
  169.      * 最后一页
  170.      * @param $name
  171.      * @return string
  172.      */
  173.     public function last_page($name = '最后一页')
  174.     {
  175.                 if($this->now_page < $this->total_pages - 5)
  176.                 {
  177.                         return $this->_get_link($this->total_pages, $name);
  178.                 }      
  179.                 return '';
  180.     }  
  181.        
  182.     /**
  183.      * 上一页
  184.      * @return string
  185.      */
  186.     public function up_page($name = '上一页')
  187.     {
  188.         if($this->now_page != 1)
  189.         {
  190.                 return $this->_get_link($this->now_page - 1, $name);
  191.         }
  192.         return '';
  193.     }
  194.        
  195.     /**
  196.      * 下一页
  197.      * @return string
  198.      */
  199.     public function down_page($name = '下一页')
  200.     {
  201.         if($this->now_page < $this->total_pages)
  202.         {
  203.                 return $this->_get_link($this->now_page + 1, $name);
  204.         }
  205.         return '';
  206.     }
  207.  
  208.     /**
  209.      * 分页样式输出
  210.      * @param $param
  211.      * @return string
  212.      */
  213.     public function show($param = 1)
  214.     {
  215.         if($this->total_rows < 1)
  216.         {
  217.                 return '';
  218.         }
  219.        
  220.         $className = 'show_' . $param;
  221.        
  222.         $classNames = get_class_methods($this);
  223.  
  224.         if(in_array($className, $classNames))
  225.         {
  226.                 return $this->$className();
  227.         }
  228.                 return '';
  229.     }
  230.    
  231.     protected function show_1()
  232.     {
  233.                 $plus = $this->plus;
  234.         if( $plus + $this->now_page > $this->total_pages)
  235.         {
  236.                 $begin = $this->total_pages - $plus * 2;
  237.         }else{
  238.                 $begin = $this->now_page - $plus;
  239.         }
  240.        
  241.         $begin = ($begin >= 1) ? $begin : 1;
  242.         $return = '';
  243.         $return .= $this->first_page();
  244.         $return .= $this->up_page();
  245.         for ($i = $begin; $i <= $begin + $plus * 2;$i++)
  246.         {
  247.                 if($i>$this->total_pages)
  248.                 {
  249.                         break;
  250.                 }
  251.                 if($i == $this->now_page)
  252.                 {
  253.                         $return .= "<a class='now_page'>$i</a>\n";
  254.                 }
  255.                 else
  256.                 {
  257.                         $return .= $this->_get_link($i, $i) . "\n";
  258.                 }
  259.         }
  260.         $return .= $this->down_page();
  261.         $return .= $this->last_page();
  262.         return $return;
  263.     }
  264.        
  265.         protected function show_2()
  266.     {
  267.         if($this->total_pages != 1)
  268.         {
  269.                 $return = '';
  270.                         $return .= '<div class="pagetxt">共<b>&nbsp;' .$this->total_pages. '&nbsp;</b>页&nbsp;&nbsp;当前第<b>&nbsp;' .$this->now_page. '&nbsp;</b>页, 共有记录<b>&nbsp;' .$this->total_rows. '&nbsp;</b>条</div><div class="pagea">';
  271.                 $return .= $this->up_page('上一页');
  272.                         for($i = 1;$i<=$this->total_pages;$i++)
  273.                         {
  274.                                 if($i == $this->now_page)
  275.                                 {
  276.                                         $return .= "<a class='now_page'>$i</a>\n";
  277.                                 }
  278.                                 else
  279.                                 {
  280.                                         if($this->now_page-$i>=4 && $i != 1)
  281.                                         {
  282.                                                 $return .="<span class='pageMore'>...</span>\n";
  283.                                                 $i = $this->now_page-3;
  284.                                         }
  285.                                         else
  286.                                         {
  287.                                                 if($i >= $this->now_page+5 && $i != $this->total_pages)
  288.                                                 {
  289.                                                         $return .="<span>...</span>\n";
  290.                                                         $i = $this->total_pages;
  291.                                                 }
  292.                                                 $return .= $this->_get_link($i, $i) . "\n";
  293.                                         }
  294.                                 }
  295.                         }
  296.                         $return .= $this->down_page('下一页');
  297.                         $return .= '</div>';
  298.                 return $return;
  299.         }
  300.     }
  301.    
  302.     protected function show_3()
  303.     {
  304.         $plus = $this->plus;
  305.         if( $plus + $this->now_page > $this->total_pages)
  306.         {
  307.                 $begin = $this->total_pages - $plus * 2;
  308.         }else{
  309.                 $begin = $this->now_page - $plus;
  310.         }      
  311.         $begin = ($begin >= 1) ? $begin : 1;
  312.         $return = '总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';
  313.         $return .= ',每页 ';
  314.         $return .= '<input type="text" value="'.$this->list_rows.'" id="pageSize" size="3"> ';
  315.         $return .= $this->first_page()."\n";
  316.         $return .= $this->up_page()."\n";
  317.         $return .= $this->down_page()."\n";
  318.         $return .= $this->last_page()."\n";
  319.         $return .= '<select onchange="'.$this->ajax_func_name.'(this.value)" id="gotoPage">';
  320.        
  321.         for ($i = $begin;$i<=$begin+10;$i++)
  322.         {
  323.             if($i>$this->total_pages)
  324.                 {
  325.                         break;
  326.                 }              
  327.                 if($i == $this->now_page)
  328.                 {
  329.                         $return .= '<option selected="true" value="'.$i.'">'.$i.'</option>';
  330.                 }
  331.                 else
  332.                 {
  333.                         $return .= '<option value="' .$i. '">' .$i. '</option>';
  334.                 }              
  335.         }
  336.          $return .= '</select>';
  337.         return $return;
  338.     }
  339.        
  340.         protected function show_4()
  341.     {
  342.                 $plus = $this->plus;
  343.         if( $plus + $this->now_page > $this->total_pages)
  344.         {
  345.                 $begin = $this->total_pages - $plus * 2;
  346.         }else{
  347.                 $begin = $this->now_page - $plus;
  348.         }
  349.        
  350.         $begin = ($begin >= 1) ? $begin : 1;
  351.         $return = '';
  352.                 $return .= '<div class="pagetxt">共<b>&nbsp;' .$this->total_pages. '&nbsp;</b>页&nbsp;&nbsp;当前第<b>&nbsp;' .$this->now_page. '&nbsp;</b>页, 共有记录<b>&nbsp;' .$this->total_rows. '&nbsp;</b>条</div><div class="pagea">';
  353.         $return .= $this->first_page();
  354.         $return .= $this->up_page();
  355.         for ($i = $begin; $i <= $begin + $plus * 2;$i++)
  356.         {
  357.                 if($i>$this->total_pages)
  358.                 {
  359.                         break;
  360.                 }
  361.                 if($i == $this->now_page)
  362.                 {
  363.                         $return .= "<a class='now_page'>$i</a>\n";
  364.                 }
  365.                 else
  366.                 {
  367.                         $return .= $this->_get_link($i, $i) . "\n";
  368.                 }
  369.         }
  370.         $return .= $this->down_page();
  371.         $return .= $this->last_page();
  372.                 $return .= '</div>';
  373.         return $return;
  374.     }
  375. }
  376. ?>
  377.  

回复 "PHP分页类,有四种样式共选择"

这儿你可以回复上面这条便签

captcha