一个简易的php分页类(附带样式)
1 <?php 2 // +---------------------------------------------------------------------- 3 // | Copyright (c) 2006~2017 http://baiyf.com All rights reserved. 4 // +---------------------------------------------------------------------- 5 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) 6 // +---------------------------------------------------------------------- 7 // | Author: NickBai <1902822973@qq.com> 8 // +---------------------------------------------------------------------- 9 class Page 10 { 11 private $total = 0; //总数 12 private $pageSize = 0; //每页显示的条数 13 private $page = 'page'; //接收的变量名 14 private $pageNumber = 0; //分的页数 15 private $window = 5; //默认显示的页码个数 16 private $currentPage = 0; //当前页 17 //架构函数 18 //初始化分页信息 19 public function __construct( $total, $pageSize ) 20 { 21 $this->total = $total; 22 $this->pageSize = $pageSize; 23 $this->pageNumber = ceil( $total / $pageSize ); 24 $this->currentPage = isset( $_GET[$this->page] ) ? $_GET[$this->page] : 1; //get获取当前页 25 } 26 //渲染分页html 27 public function show() 28 { 29 $slider = null; 30 $side = 3; 31 $window = $side + 2; 32 if ($this->pageNumber < $window) { 33 $slider = $this->getUrlRange(1, $this->pageNumber); 34 } elseif ($this->currentPage < $window) { 35 $slider = $this->getUrlRange(1, $window); 36 } elseif ($this->currentPage > ($this->pageNumber - $side)) { 37 $slider = $this->getUrlRange($this->currentPage - 2, $this->pageNumber); 38 } else { 39 $slider = $this->getUrlRange($this->currentPage - 2, $this->currentPage + 2); 40 } 41 $html = '<ul class="pagination">'; 42 $html .= $this->getPreButton(); 43 if (is_array($slider)) { 44 $html .= $this->getUrlLinks($slider); 45 } 46 $html .= $this->getNextButton(); 47 $html .= '</ul>'; 48 return $html; 49 } 50 //获取可点击的页码 51 private function getPageLink( $url, $page ) 52 { 53 if( $page == $this->currentPage ){ 54 return $this->getActivePage( $page ); 55 } 56 return '<li><a href="' . htmlentities( $url ) . '">' . $page . '</a></li>'; 57 } 58 //获取不可点击的页码 59 private function getDisablePage( $text ) 60 { 61 return '<li class="disabled"><span>' . $text . '</span></li>'; 62 } 63 //获取选中的样式 64 private function getActivePage( $text ) 65 { 66 return '<li class="active"><span>' . $text . '</span></li>'; 67 } 68 //创建一组分页链接 69 private function getUrlRange( $start, $end ) 70 { 71 $urls = []; 72 for ($page = $start; $page <= $end; $page++) { 73 $urls[$page] = $this->buildUrl($page); 74 } 75 return $urls; 76 } 77 //批量生成页码按钮. 78 protected function getUrlLinks(array $urls) 79 { 80 $html = ''; 81 foreach ($urls as $page => $url) { 82 $html .= $this->getCommonPage($url, $page); 83 } 84 return $html; 85 } 86 //组件url参数 87 private function buildUrl($text) 88 { 89 return '?' . $this->page . '='.$text; 90 } 91 //获取普通的分页按钮 92 private function getCommonPage( $url, $text ) 93 { 94 return $this->getPageLink( $url, $text ); 95 } 96 //获取上一页按钮 97 private function getPreButton($text='上一页') 98 { 99 if( $this->currentPage <= 1 ){ 100 101 return $this->getDisablePage($text); 102 } 103 $url = $this->buildUrl( $this->currentPage - 1 ); 104 return $this->getPageLink( $url, $text); 105 } 106 //获取下一页按钮 107 private function getNextButton($text='下一页') 108 { 109 if( $this->currentPage == $this->pageNumber ){ 110 111 return $this->getDisablePage($text); 112 } 113 $url = $this->buildUrl( $this->currentPage + 1 ); 114 return $this->getPageLink( $url, $text); 115 } 116 } 117 ?> 118 119 <!DOCTYPE html> 120 <html> 121 <head> 122 <title>分页码</title> 123 <meta charset="utf-8" /> 124 </head> 125 <body> 126 <style> 127 128 .pagination { 129 height: 40px; 130 margin: 0 auto; 131 width: 470px; 132 } 133 .pagination li { 134 border: 1px solid #d9d9d9; 135 float: left; 136 font-size: 14px; 137 height: 40px; 138 line-height: 40px; 139 margin-left: 5px; 140 padding: 0 18px; 141 list-style:none; 142 } 143 .pagination li a { 144 color: #2f2f2f; 145 text-decoration:none; 146 } 147 .active { 148 background: #2bb7ff none repeat scroll 0 0; 149 color: #fff; 150 } 151 </style> 152 <?php 153 $page = new Page(100, 10); 154 echo $page->show(); 155 ?> 156 157 </body> 158 </html>
声明:本文内容仅是本人学习的记录,不保证在项目中可用,若引用此代码导致了严重后果,本人不承担任何法律责任。