Home > php教程 > php手册 > 【推荐】一个解决自定义路由的分页类

【推荐】一个解决自定义路由的分页类

WBOY
Release: 2016-06-07 11:38:05
Original
1047 people have browsed it

【推荐】一个解决自定义路由的分页类
 //----------------------------------------------------------<br>  //声明: 本代码并非完美,也许存在不佳之处,请放肆吐槽!<br>  //作者: 小曾<br>  //扣扣: 839024615<br>  //网址: www.yun8888.net<br>  //关于我的ThinkPHP http://www.thinkphp.cn/u/87696.html<br>  //----------------------------------------------------------<br> <br> <br> <br> <br> <br> //好多人在问,假如我们给列表重新定义的路由<br> //那么原本地址是 list/index/id/1.html<br> //定义后 list/1.html <br> //当你在系统分页类时,他又会跳到 list/index/id/1/p/2.html<br> //我们重新弄个类就ok了<br> <br> <br> //用法<br> <br>             vendor('Page.Page#class');<br>             $params = array(<br>             'total_rows'=>$count, #(必须)<br>             'method'    =>'html', #(必须)<br>             'parameter' =>'/list/'.$id.'/p/?.html',  #(必须)<br>             'now_page'  =>$p,  #(必须)<br>             'list_rows' =>30, #(可选) 默认为15<br>         );<br> <br>             $page = new Core_Lib_Page($params);<br>         <br>             $this->assign("page",$page->show(1));<br> <br> <br> <br> <br> <br> <br> <br> <br> /**<br>  * 分页类<br>  * @author  xiaojiong & 290747680@qq.com<br>  * @date 2011-08-17<br>  * <br>  * show(2)  1 ... 62 63 64 65 66 67 68 ... 150<br>  * 分页样式 <br>  * #page{font:12px/16px arial}<br>  * #page span{float:left;margin:0px 3px;}<br>  * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}<br>  * #page a.now_page,#page a:hover{color:#fff;background:#05c}<br> */<br>  <br> class Core_Lib_Page<br> {<br>     public     $first_row;        //起始行数<br>  <br>     public     $list_rows;        //列表每页显示行数<br>      <br>     protected  $total_pages;      //总页数<br>  <br>     protected  $total_rows;       //总行数<br>      <br>     protected  $now_page;         //当前页数<br>      <br>     protected  $method  = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式 <br>      <br>     protected  $parameter = '';<br>      <br>     protected  $page_name;        //分页参数的名称<br>      <br>     protected  $ajax_func_name;<br>      <br>     public     $plus = 3;         //分页偏移量<br>      <br>     protected  $url;<br>      <br>      <br>     /**<br>      * 构造函数<br>      * @param unknown_type $data<br>      */<br>     public function __construct($data = array())<br>     {<br>         $this->total_rows = $data['total_rows'];<br>  <br>         $this->parameter         = !empty($data['parameter']) ? $data['parameter'] : '';<br>         $this->list_rows         = !empty($data['list_rows']) && $data['list_rows']          $this->total_pages       = ceil($this->total_rows / $this->list_rows);<br>         $this->page_name         = !empty($data['page_name']) ? $data['page_name'] : 'p';<br>         $this->ajax_func_name    = !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : '';<br>          <br>         $this->method           = !empty($data['method']) ? $data['method'] : '';<br>          <br>          <br>         /* 当前页面 */<br>         if(!empty($data['now_page']))<br>         {<br>             $this->now_page = intval($data['now_page']);<br>         }else{<br>             $this->now_page   = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;<br>         }<br>         $this->now_page   = $this->now_page now_page;<br>      <br>          <br>         if(!empty($this->total_pages) && $this->now_page > $this->total_pages)<br>         {<br>             $this->now_page = $this->total_pages;<br>         }<br>         $this->first_row = $this->list_rows * ($this->now_page - 1);<br>     }   <br>      <br>     /**<br>      * 得到当前连接<br>      * @param $page<br>      * @param $text<br>      * @return string<br>      */<br>     protected function _get_link($page,$text)<br>     {<br>         switch ($this->method) {<br>             case 'ajax':<br>                 $parameter = '';<br>                 if($this->parameter)<br>                 {<br>                     $parameter = ','.$this->parameter;<br>                 }<br>                 return '<a>ajax_func_name . '(\'' . $page . '\''.$parameter.')" href="javascript:void(0)">' . $text . '</a>' . "\n";<br>             break;<br>              <br>             case 'html':<br>                 $url = str_replace('?', $page,$this->parameter);<br>                 return '<a>' . $text . '</a>' . "\n";<br>             break;<br>              <br>             default:<br>                 return '<a>_get_url($page) . '">' . $text . '</a>' . "\n";<br>             break;<br>         }<br>     }<br>      <br>      <br>     /**<br>      * 设置当前页面链接<br>      */<br>     protected function _set_url()<br>     {<br>         $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;<br>         $parse = parse_url($url);<br>         if(isset($parse['query'])) {<br>             parse_str($parse['query'],$params);<br>             unset($params[$this->page_name]);<br>             $url   =  $parse['path'].'?'.http_build_query($params);<br>         }<br>         if(!empty($params))<br>         {<br>             $url .= '&';<br>         }<br>         $this->url = $url;<br>     }<br>      <br>     /**<br>      * 得到$page的url<br>      * @param $page 页面<br>      * @return string<br>      */<br>     protected function _get_url($page)<br>     {<br>         if($this->url === NULL)<br>         {<br>             $this->_set_url();   <br>         }<br>     //  $lable = strpos('&', $this->url) === FALSE ? '' : '&';<br>         return $this->url . $this->page_name . '=' . $page;<br>     }<br>      <br>      <br>     /**<br>      * 得到第一页<br>      * @return string<br>      */<br>     public function first_page($name = '第一页')<br>     {<br>         if($this->now_page > 5)<br>         {<br>             return $this->_get_link('1', $name);<br>         }   <br>         return '';<br>     }<br>      <br>     /**<br>      * 最后一页<br>      * @param $name<br>      * @return string<br>      */<br>     public function last_page($name = '最后一页')<br>     {<br>         if($this->now_page total_pages - 5)<br>         {<br>             return $this->_get_link($this->total_pages, $name);<br>         }   <br>         return '';<br>     }  <br>      <br>     /**<br>      * 上一页<br>      * @return string<br>      */<br>     public function up_page($name = '上一页')<br>     {<br>         if($this->now_page != 1)<br>         {<br>             return $this->_get_link($this->now_page - 1, $name);<br>         }<br>         return '';<br>     }<br>      <br>     /**<br>      * 下一页<br>      * @return string<br>      */<br>     public function down_page($name = '下一页')<br>     {<br>         if($this->now_page total_pages)<br>         {<br>             return $this->_get_link($this->now_page + 1, $name);<br>         }<br>         return '';<br>     }<br>  <br>     /**<br>      * 分页样式输出<br>      * @param $param<br>      * @return string<br>      */<br>     public function show($param = 1)<br>     {<br>         if($this->total_rows          {<br>             return '';<br>         }<br>          <br>         $className = 'show_' . $param;<br>          <br>         $classNames = get_class_methods($this);<br>  <br>         if(in_array($className, $classNames))<br>         {<br>             return $this->$className();<br>         }<br>         return '';<br>     }<br>      <br>     protected function show_2()<br>     {<br>         if($this->total_pages != 1)<br>         {<br>             $return = '';<br>             $return .= $this->up_page('             for($i = 1;$itotal_pages;$i++)<br>             {<br>                 if($i == $this->now_page)<br>                 {<br>                     $return .= "<a>$i</a>\n";<br>                 }<br>                 else<br>                 {<br>                     if($this->now_page-$i>=4 && $i != 1)<br>                     {<br>                         $return .="<span>...</span>\n";<br>                         $i = $this->now_page-3;<br>                     }<br>                     else<br>                     {<br>                         if($i >= $this->now_page+5 && $i != $this->total_pages)<br>                         {<br>                             $return .="<span>...</span>\n"; <br>                             $i = $this->total_pages;<br>                         }<br>                         $return .= $this->_get_link($i, $i) . "\n";<br>                     }<br>                 }<br>             }<br>             $return .= $this->down_page('>');<br>             return $return;<br>         }<br>     }<br>      <br>     protected function show_1()<br>     {<br>         $plus = $this->plus;<br>         if( $plus + $this->now_page > $this->total_pages)<br>         {<br>             $begin = $this->total_pages - $plus * 2;<br>         }else{<br>             $begin = $this->now_page - $plus;<br>         }<br>          <br>         $begin = ($begin >= 1) ? $begin : 1;<br>         $return = '';<br>         $return .= $this->first_page();<br>         $return .= $this->up_page();<br>         for ($i = $begin; $i          {<br>             if($i>$this->total_pages)<br>             {<br>                 break;<br>             }<br>             if($i == $this->now_page)<br>             {<br>                 $return .= "<a>$i</a>\n";<br>             }<br>             else<br>             {<br>                 $return .= $this->_get_link($i, $i) . "\n";<br>             }<br>         }<br>         $return .= $this->down_page();<br>         $return .= $this->last_page();<br>         return $return;<br>     }<br>      <br>     protected function show_3()<br>     {<br>         $plus = $this->plus;<br>         if( $plus + $this->now_page > $this->total_pages)<br>         {<br>             $begin = $this->total_pages - $plus * 2;<br>         }else{<br>             $begin = $this->now_page - $plus;<br>         }       <br>         $begin = ($begin >= 1) ? $begin : 1;<br>         $return = '总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';<br>         $return .= ',每页 ';<br>         $return .= '<input>list_rows.'" id="pageSize" size="3"> ';<br>         $return .= $this->first_page()."\n";<br>         $return .= $this->up_page()."\n"; <br>         $return .= $this->down_page()."\n";<br>         $return .= $this->last_page()."\n";<br>         $return .= '<select>ajax_func_name.'(this.value)" id="gotoPage">';<br>         <br>         for ($i = $begin;$i         {<br>             if($i>$this->total_pages)<br>             {<br>                 break;<br>             }           <br>             if($i == $this->now_page)<br>             {<br>                 $return .= '<option>'.$i.'</option>';<br>             }<br>             else<br>             {<br>                 $return .= '<option>' .$i. '</option>';<br>             }           <br>         }<br>          $return .= '</select>';<br>         return $return;<br>     }<br> }<br> <br> <br> <br> <br> <br> <br> <br>  //----------------------------------------------------------<br>  //声明: 本代码并非完美,也许存在不佳之处,请放肆吐槽!<br>  //作者: 小曾<br>  //扣扣: 839024615<br>  //网址: www.yun8888.net<br>  //关于我的ThinkPHP http://www.thinkphp.cn/u/87696.html<br>  //----------------------------------------------------------

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template