[PHP] php分页类 →→→→→进入此内容的聊天室

来自 , 2020-03-07, 写在 PHP, 查看 105 次.
URL http://www.code666.cn/view/1397386b
  1. <?php
  2. //====================================================
  3. //              FileName: page.class.php
  4. //              Summary:  系统分页配置
  5. //====================================================
  6.  
  7. class page {
  8.         /**
  9.         * config ,public
  10.         */
  11.         var $page_name="pageNum";//page标签,用来控制url页。比如说xxx.php?PB_page=2中的PB_page
  12.         var $next_page='>';//下一页
  13.         var $pre_page='<';//上一页
  14.         var $first_page='First';//首页
  15.         var $last_page='Last';//尾页
  16.         var $pre_bar='<<';//上一分页条
  17.         var $next_bar='>>';//下一分页条
  18.         var $format_left=' ';
  19.         var $format_right=' ';
  20.         var $is_ajax=false;//是否支持AJAX分页模式
  21.  
  22.         /**
  23.         * private
  24.         *
  25.         */
  26.         var $pagebarnum=10;//控制记录条的个数。
  27.         var $totalpage=0;//总页数
  28.         var $ajax_action_name='';//AJAX动作名
  29.         var $nowindex=1;//当前页
  30.         var $url="";//url地址头
  31.         var $offset=0;
  32.  
  33.         /**
  34.         * constructor构造函数
  35.         *
  36.         * @param array $array['total'],$array['perpage'],$array['nowindex'],$array['url'],$array['ajax']...
  37.         */
  38.         function page($array){
  39.            if(is_array($array)){
  40.                 if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
  41.                 $total=intval($array['total']);
  42.                 $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
  43.                 $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
  44.                 $url=(array_key_exists('url',$array))?$array['url']:'';
  45.            }else{
  46.                 $total=$array;
  47.                 $perpage=10;
  48.                 $nowindex='';
  49.                 $url='';
  50.            }
  51.            if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
  52.            if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
  53.            if(!empty($array['page_name']))$this->set('page_name',$array['page_name']);//设置pagename
  54.            $this->_set_nowindex($nowindex);//设置当前页
  55.            $this->_set_url($url);//设置链接地址
  56.            $this->totalpage=ceil($total/$perpage);
  57.            $this->offset=($this->nowindex-1)*$this->perpage;
  58.            if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
  59.         }
  60.        
  61.         /**
  62.         * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception
  63.         *
  64.         * @param string $var
  65.         * @param string $value
  66.         */
  67.         function set($var,$value){
  68.            if(in_array($var,get_object_vars($this)))
  69.                 $this->$var=$value;
  70.            else {
  71.         $this->error(__FUNCTION__,$var." does not belong to PB_Page!");
  72.            }
  73.          
  74.         }
  75.        
  76.         /**
  77.         * 打开倒AJAX模式
  78.         *
  79.         * @param string $action 默认ajax触发的动作。
  80.         */
  81.         function open_ajax($action){
  82.            $this->is_ajax=true;
  83.            $this->ajax_action_name=$action;
  84.         }
  85.        
  86.         /**
  87.         * 获取显示"下一页"的代码
  88.         *
  89.         * @param string $style
  90.         * @return string
  91.         */
  92.         function next_page($style=''){
  93.            if($this->nowindex<$this->totalpage){
  94.                 return "<li>".$this->_get_link($this->_get_url($this->nowindex+1),$this->next_page,$style)."</li>";
  95.            }
  96.            return "<li class='active'><span>".$this->next_page."</span></li>";
  97.         }
  98.  
  99.         /**
  100.         * 获取显示“上一页”的代码
  101.         *
  102.         * @param string $style
  103.         * @return string
  104.         */
  105.         function pre_page($style=''){
  106.            if($this->nowindex>1){
  107.         return "<li>".$this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style)."</li>";
  108.            }
  109.            return "<li class='active'><span>".$this->pre_page."</span></li>";
  110.         }
  111.  
  112.         /**
  113.         * 获取显示“首页”的代码
  114.         *
  115.         * @return string
  116.         */
  117.         function first_page($style=''){
  118.            if($this->nowindex==1){
  119.            return "<li class='active'><span>".$this->first_page."</span></li>";
  120.            }
  121.            return "<li>".$this->_get_link($this->_get_url(1),$this->first_page,$style)."</li>";
  122.         }
  123.  
  124.         /**
  125.         * 获取显示“尾页”的代码
  126.         *
  127.         * @return string
  128.         */
  129.         function last_page($style='lastpage'){
  130.            if($this->nowindex==$this->totalpage){
  131.            return "<li class='active'><span>".$this->last_page."</span></li>";
  132.            }
  133.            return "<li>".$this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style)."</li>";
  134.         }
  135.  
  136.         function nowbar($style='',$nowindex_style='current'){
  137.            $plus=ceil($this->pagebarnum/2);
  138.            if($this->pagebarnum-$plus+$this->nowindex>$this->totalpage)$plus=($this->pagebarnum-$this->totalpage+$this->nowindex);
  139.            $begin=$this->nowindex-$plus+1;
  140.            $begin=($begin>=1)?$begin:1;
  141.            $return='';
  142.            for($i=$begin;$i<$begin+$this->pagebarnum;$i++)
  143.            {
  144.         if($i<=$this->totalpage){
  145.         if($i!=$this->nowindex)
  146.                    $return.=$this->_get_text("<li>".$this->_get_link($this->_get_url($i),$i,$style)."</li>");
  147.         else
  148.                    $return.=$this->_get_text("<li class='active'><span class='".$nowindex_style."'>".$i."</span></li>");
  149.         }else{
  150.         break;
  151.         }
  152.         $return.="\n";
  153.            }
  154.            unset($begin);
  155.            return $return;
  156.         }
  157.        
  158.         /**
  159.         * 获取显示跳转按钮的代码
  160.         *
  161.         * @return string
  162.         */
  163.         function select(){
  164.            $return="<select name=\"jumpMenu\" id=\"jumpMenu\" onchange=\"MM_jumpMenu('window',this,0)\">";
  165.            for($i=1;$i<=$this->totalpage;$i++)
  166.            {
  167.         if($i==$this->nowindex){
  168.                 if(strip_tags($_GET[cid],'')==""){
  169.                 $return.="<option value=\"?" .$this->page_name . "=".$i."\" selected>".$i."</option>";
  170.                 }else{
  171.                 $return.="<option value=\"?cid=".strip_tags($_GET[cid],'')."&" .$this->page_name . "=".$i."\" selected>".$i."</option>";
  172.                 }
  173.         }else{
  174.                 if(strip_tags($_GET[cid],'')==""){
  175.                 $return.="<option value=\"?" .$this->page_name . "=".$i."\">".$i."</option>";
  176.                 }else{
  177.                 $return.="<option value=\"?cid=".strip_tags($_GET[cid],'')."&" .$this->page_name . "=".$i."\" >".$i."</option>";
  178.                 }
  179.  
  180.         }
  181.            }
  182.            unset($i);
  183.            $return.='</select>';
  184.            return $return;
  185.         }
  186.  
  187.         /**
  188.         * 获取mysql 语句中limit需要的值
  189.         *
  190.         * @return string
  191.         */
  192.         function offset(){
  193.            return $this->offset;
  194.         }
  195.  
  196.         /**
  197.         * 控制分页显示风格(你可以增加相应的风格)
  198.         *
  199.         * @param int $mode
  200.         * @return string
  201.         */
  202.         function show($mode=1){
  203.            switch ($mode)
  204.            {
  205.         case '1':
  206.         $this->first_page='首页';
  207.         $this->last_page='尾页';
  208.         $this->next_page='下一页';
  209.         $this->pre_page='上一页';
  210.         return $this->first_page()." ".$this->pre_page()." ".$this->nowbar()." ".$this->next_page()." ".$this->last_page();
  211.         break;
  212.         case '2':
  213.         $this->next_page='下一页';
  214.         $this->pre_page='上一页';
  215.         $this->first_page='首页';
  216.         $this->last_page='尾页';
  217.         return $this->first_page().$this->pre_page().'[第'.$this->nowindex.'页]'.$this->next_page().$this->last_page().'第'.$this->select().'页';
  218.         break;
  219.         case '3':
  220.         $this->next_page='下一页';
  221.         $this->pre_page='上一页';
  222.         $this->first_page='首页';
  223.         $this->last_page='尾页';
  224.         return $this->first_page().$this->pre_page().$this->next_page().$this->last_page();
  225.         break;
  226.         case '4':
  227.         $this->next_page='下一页';
  228.         $this->pre_page='上一页';
  229.         return $this->pre_page().$this->nowbar().$this->next_page();
  230.         break;
  231.         case '5':
  232.         return $this->pre_bar().$this->pre_page().$this->nowbar().$this->next_page().$this->next_bar();
  233.         break;
  234.            }
  235.         }
  236.        
  237.         /*----------------private function (私有方法)-----------------------------------------------------------*/
  238.         /**
  239.         * 设置url头地址
  240.         * @param: String $url
  241.         * @return boolean
  242.         */
  243.         function _set_url($url=""){
  244.            if(!empty($url)){
  245.            //手动设置
  246.         $this->url=$url.((stristr($url,'?'))?'&':'?').$this->page_name."=";
  247.            }else{
  248.            //自动获取
  249.         if(empty($_SERVER['QUERY_STRING'])){
  250.            //不存在QUERY_STRING时
  251.         $this->url=$_SERVER['REQUEST_URI']."?".$this->page_name."=";
  252.         }else{
  253.            //
  254.         if(stristr($_SERVER['QUERY_STRING'],$this->page_name.'=')){
  255.                    //地址存在页面参数
  256.                 $this->url=str_replace($this->page_name.'='.$this->nowindex,'',$_SERVER['REQUEST_URI']);
  257.                 $last=$this->url[strlen($this->url)-1];
  258.                 if($last=='?'||$last=='&'){
  259.                   $this->url.=$this->page_name."=";
  260.                 }else{
  261.                   $this->url.='&'.$this->page_name."=";
  262.                 }
  263.         }else{
  264.                    //
  265.                 $this->url=$_SERVER['REQUEST_URI'].'&'.$this->page_name.'=';
  266.         }
  267.         }
  268.            }
  269.         }
  270.  
  271.         /**
  272.         * 设置当前页面
  273.         *
  274.         */
  275.         function _set_nowindex($nowindex){
  276.            if(empty($nowindex)){
  277.         //系统获取
  278.  
  279.         if(isset($_GET[$this->page_name])){
  280.         $this->nowindex=intval($_GET[$this->page_name]);
  281.         }
  282.            }else{
  283.            //手动设置
  284.         $this->nowindex=intval($nowindex);
  285.            }
  286.         }
  287.          
  288.         /**
  289.         * 为指定的页面返回地址值
  290.         *
  291.         * @param int $pageno
  292.         * @return string $url
  293.         */
  294.         function _get_url($pageno=1){
  295.            return $this->url.$pageno;
  296.         }
  297.  
  298.         /**
  299.         * 获取分页显示文字,比如说默认情况下_get_text('<a href="">1</a>')将返回[<a href="">1</a>]
  300.         *
  301.         * @param String $str
  302.         * @return string $url
  303.         */
  304.         function _get_text($str){
  305.            return $this->format_left.$str.$this->format_right;
  306.         }
  307.  
  308.         /**
  309.         * 获取链接地址
  310.         */
  311.         function _get_link($url,$text,$style=''){
  312.            $style=(empty($style))?'':'class="'.$style.'"';
  313.            if($this->is_ajax){
  314.            //如果是使用AJAX模式
  315.         return '<a '.$style.' href="javascript:'.$this->ajax_action_name.'(\''.$url.'\')">'.$text.'</a>';
  316.            }else{
  317.         return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
  318.            }
  319.         }
  320.         /**
  321.         * 出错处理方式
  322.         */
  323.         function error($function,$errormsg){
  324.                 die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
  325.         }
  326. }
  327. ?>

回复 "php分页类"

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

captcha