自定义MVC框架之工具类-分页类的封装

以前写过一个MVC框架,封装的有点low,经过一段时间的沉淀,打算重新改造下,之前这篇文章封装过一个验证码类。

这次重新改造MVC有几个很大的收获

>全部代码都是用Ubuntu+Vim编写,以前都是windows上开发,这次彻底迷上Ubuntu Linux

>裸装php,用php自带的服务器解释执行php,缺哪个扩展就装哪个,最后通过整个MVC框架的开发,把Lamp所有的常用配置与细节搞懂

>通过扩展安装,学习扩展开发与php底层源码分析

总之,终于感觉层次又提升了不少。

分页类代码:

  1 <?php
  2     
  3 class Page {
  4     //每页显示的条目
  5     protected $pageSize;    
  6     //总记录数
  7     protected $totalRecord;
  8     //当前页
  9     protected $p;
 10     //总页数
 11     protected $totalPage;
 12     protected $url;
 13 
 14     public function __construct( $_pageSize, $_totalRecord ){
 15         $this->pageSize = $_pageSize;
 16         $this->totalRecord = $_totalRecord;        
 17         $this->totalPage = ceil( $this->totalRecord / $this->pageSize );
 18         $this->p = $this->getCurPage();
 19         $this->url = $this->getUrl();
 20     }
 21 
 22     public function setUrl( $p ){
 23         if( strstr( $this->url, '?' ) ) {
 24             //url中有参数
 25             $url = $this->url . '&p=' . $p;
 26         }else {
 27             //url中没有参数
 28             $url = $this->url . '?p=' . $p;
 29         }
 30         return $url;
 31     }
 32 
 33     //首页
 34     public function firstPage(){
 35         return $this->setUrl( 1 );
 36     }
 37 
 38     //末页
 39     public function lastPage(){
 40         return $this->setUrl( $this->totalPage );
 41     }
 42 
 43     //上一页
 44     public function prevPage(){
 45         if( $this->p - 1 <= 0 ) {
 46             $prevPage = $this->p;
 47         }else {
 48             $prevPage = $this->p - 1;
 49         }
 50         return $this->setUrl( $prevPage );
 51     }
 52 
 53     //下一页
 54     public function nextPage(){
 55         if( $this->p + 1 > $this->totalPage ) {
 56             $nextPage = $this->p;
 57         }else {
 58             $nextPage = $this->p + 1;
 59         }
 60         return $this->setUrl( $nextPage );
 61     }
 62 
 63     //得到当前的页码
 64     public function getCurPage(){
 65         $curPage = intval( $_GET['p'] );
 66         if ( empty( $curPage ) ) {
 67             $curPage = 1;
 68         }else if ( $curPage > $this->totalPage ) {
 69             $curPage = $this->totalPage;
 70         }else if ( $curPage < 0 ){
 71             $curPage = 1;
 72         }
 73         return $curPage;
 74     }
 75 
 76     //拼接url
 77     public function getUrl(){
 78         $protocol = strtolower( array_shift( explode( '/', $_SERVER['SERVER_PROTOCOL'] ) ) );
 79         $host = $_SERVER['SERVER_NAME'];
 80         $port = $_SERVER['SERVER_PORT'];
 81         $uri = $_SERVER['REQUEST_URI'];
 82         $uriArr = parse_url( $uri );
 83         $path = $uriArr['path'];
 84         if( !empty( $uriArr['query'] ) ) {
 85             //url中的query字符转数组
 86             parse_str( $uriArr['query'], $args );
 87             //清除原来的分页参数p
 88             if( isset( $args['p'] ) ){
 89                 unset( $args['p'] );
 90             }
 91             //参数重新拼接成字符串
 92             $queryString = http_build_query( $args );
 93             //字符串如果不止一个p参数,把那些参数拼接在path的后面
 94             if ( !empty( $queryString ) ){
 95                 $path .= '?' . $queryString;
 96             }
 97         }
 98         return $protocol . '://' . $host . ':' . $port . $path;
 99     }
100 
101     public function render(){
102         return [
103             'first' => $this->firstPage(),
104             'last' => $this->lastPage(),
105             'prev' => $this->prevPage(),
106             'next' => $this->nextPage()
107         ];
108     }
109 
110     public function limit(){
111         $offset = ( $this->p - 1 ) * $this->pageSize;
112         return $offset . ',' . $this->pageSize;
113     }
114 }
115 
116 $page = new Page( 5, 11 );
117 //echo $page->getCurPage();
118 //echo $page->getUrl();
119 print_r( $page->render() );
120 
121 ?>

 

posted @ 2018-02-26 22:08  ghostwu  阅读(790)  评论(1编辑  收藏  举报
Copyright ©2017 ghostwu