我第一次做的分页类,写了3种样式,就直接定死了,用了一个接口+工厂模式,写的
别的不说,先上代码!
1 <?php 2 3 /** 4 * 分页类的实现 5 * 分页样式的选择 6 * @author lvmin 7 * 8 */ 9 10 header('Content-Type:text/html;charset=utf8'); 11 /** 12 * 创建一个接口 interface show_page 代表抽象产品 实现分页类的方法 13 */ 14 interface show_page 15 { 16 public function display(); 17 } 18 19 /** 20 * 分页类的操作父类 21 * 22 * @param $total int 总的数据条数 23 * @param $page int 当前页码 24 * @param $per_page int 每页显示条数 25 * @param $page_rows int 每页显示的页码的个数 26 */ 27 class operation 28 { 29 30 protected $total = 0; //数据的总条数 31 protected $per_page = 3; //每页显示的数据条数 32 protected $page=1; //当前页 33 protected $page_rows = 5; //每页显示的页码的个数,即,若是3, //代表页面上显示1,2,3页码,其他页码再用上下页显示出来 34 protected $url = ''; //链接地址 35 protected $total_page = 0; //总页数 36 37 public function init_page($total,$page=false,$per_page=false,$page_rows=false){ 38 $this->total = intval($total); 39 if($per_page){ 40 $this->per_page = intval($per_page); 41 } 42 if($page){ 43 $this->page = intval($page); 44 } 45 if($page_rows){ 46 $this->page_rows = intval($page_rows); 47 } 48 49 50 //总页数 51 $total_page = ceil($this->total/$this->per_page); 52 53 //地址栏的参数保存 54 $uri = $_SERVER['REQUEST_URI']; 55 $parse = parse_url($uri); 56 57 $param = array(); 58 if(isset($parse['query'])){ 59 parse_str($parse['query'],$param); 60 } 61 //不管有没有都unset掉$param下的page单元,因为page是要算出来的 62 unset($param['page']); 63 64 //这么是为了将param中的page去掉 65 66 $url = $parse['path'] . '?'; 67 if(!empty($param)){ 68 $param = http_build_query($param);//得到去掉page的参数 69 $url = $url . $param . '&';//得到完整路径 70 } 71 72 $this->total_page = $total_page; 73 $this->url = $url; 74 75 } 76 77 } 78 79 /** 80 * 第一个样式 81 * 样式: [首页][上一页][1][2]3[4][5][下一页][末页] 82 */ 83 class page_css1 extends operation implements show_page 84 { 85 86 public function display(){ 87 88 $nav = array(); 89 $nav[] = "<span>" . $this->page . '</span>'; 90 91 for($left = $this->page-1,$right = $this->page+1;($left>=1||$right<=$this->total_page) && count($nav)<$this->page_rows;){ 92 93 if($left>=1){ 94 array_unshift($nav,'<a href="' . $this->url . 'page=' .$left . '">[' . $left . ']</a>'); 95 $left-=1; 96 } 97 if($right<=$this->total_page){ 98 array_push($nav, '<a href="' . $this->url . 'page=' . $right . '">[' . $right . ']</a>'); 99 $right+=1; 100 } 101 102 } 103 $pageprev = ($this->page-1>=1)? ($this->page-1):1; 104 $pagenext = ($this->page+1<=$this->total_page)? ($this->page+1):$this->total_page; 105 array_unshift($nav,'<a href="' . $this->url . 'page='. $pageprev .'">[上一页]</a>'); 106 array_unshift($nav,'<a href="' . $this->url . 'page=1">[首页]</a>'); 107 array_push($nav, '<a href="' . $this->url . 'page=' . $pagenext . '">[下一页]</a>'); 108 array_push($nav, '<a href="' . $this->url . 'page=' . $this->total_page . '">[末页]</a>'); 109 110 //var_dump($nav); 111 return implode('', $nav); 112 } 113 } 114 115 /** 116 * 第二个样式 117 * 样式: << < 1 2 3 4 5 > >> 118 */ 119 class page_css2 extends operation implements show_page 120 { 121 public function display(){ 122 123 $nav = array(); 124 $nav[] = "<span style='font-weight:bold;'><strong>" . $this->page . '</strong></span>'; 125 126 for($left = $this->page-1,$right = $this->page+1;($left>=1||$right<=$this->total_page) && count($nav)<$this->page_rows;){ 127 128 if($left>=1){ 129 array_unshift($nav,'<a href="' . $this->url . 'page=' .$left . '">' . " " . $left . " " . '</a>'); 130 $left-=1; 131 } 132 if($right<=$this->total_page){ 133 array_push($nav, '<a href="' . $this->url . 'page=' . $right . '">' . " " . $right . " " .'</a>'); 134 $right+=1; 135 } 136 137 } 138 139 $pageprev = ($this->page-1>=1)? ($this->page-1):1; 140 $pagenext = ($this->page+1<=$this->total_page)? ($this->page+1):$this->total_page; 141 array_unshift($nav,'<a href="' . $this->url . 'page='. $pageprev .'">'. "<" . '</a>'); 142 array_unshift($nav,'<a href="' . $this->url . 'page=1">'. "<<" .'</a>' . " "); 143 array_push($nav, '<a href="' . $this->url . 'page=' . $pagenext . '">'.">".'</a>' . " "); 144 array_push($nav, '<a href="' . $this->url . 'page=' . $this->total_page . '">'. ">>" .'</a>'); 145 //页码列表 146 $selector=<<<PAGE 147 <select id="pages" οnchange="window.location.href='$this->url'+'page='+this.value" > 148 PAGE; 149 //echo $url;var_dump($nav); 150 //循环页码 151 for($p=1;$p<=$this->total_page;$p++){ 152 if($this->page==$p){ 153 $selector .= <<<PAGE 154 <option value="$p" selected>$p</option> 155 PAGE; 156 }else{ 157 $selector .=<<<PAGE 158 <option value="$p">$p</option> 159 PAGE; 160 } 161 } 162 163 $selector .='</select>'; 164 165 //array_push($nav, $selector); 166 167 return implode('', $nav) . $selector; 168 } 169 } 170 171 172 /** 173 * 第三个样式 174 * 样式: 总计20记录, 分为7页, 当前第1页, 每页3条记录.首页 上一页1 2 3 4 5 下一页 尾页 175 */ 176 class page_css3 extends operation implements show_page 177 { 178 public function display(){ 179 180 181 $info = <<<PAGE 182 总计<span id="total">$this->total</span>记录, 183 分为<span id="">$this->total_page</span>页, 184 当前第<span id="">$this->page</span>页, 185 每页<span id="">$this->per_page</span>条记录 186 PAGE; 187 $nav = array(); 188 189 $nav[] = "<span style='color:#f00;'><strong>" . $this->page . '</strong></span>'; 190 191 for($left = $this->page-1,$right = $this->page+1;($left>=1||$right<=$this->total_page) && count($nav)<$this->page_rows;){ 192 193 if($left>=1){ 194 array_unshift($nav,'<a href="' . $this->url . 'page=' .$left . '">' . " " . $left . " " . '</a>'); 195 $left-=1; 196 } 197 if($right<=$this->total_page){ 198 array_push($nav, '<a href="' . $this->url . 'page=' . $right . '">' . " " . $right . " " .'</a>'); 199 $right+=1; 200 } 201 } 202 203 $pageprev = ($this->page-1>=1)? ($this->page-1):1; 204 $pagenext = ($this->page+1<=$this->total_page)? ($this->page+1):$this->total_page; 205 array_unshift($nav,'<a href="' . $this->url . 'page='. $pageprev .'">上一页</a>'); 206 array_unshift($nav,'<a href="' . $this->url . 'page=1">首页</a>' . " "); 207 array_push($nav, '<a href="' . $this->url . 'page=' . $pagenext . '">下一页</a>' . " "); 208 array_push($nav, '<a href="' . $this->url . 'page=' . $this->total_page . '">尾页</a>'); 209 array_unshift($nav, "$info"); 210 211 $jump = " 跳到<input type='text' name='jump' max_page=" .$this->total_page . " style='width:30px;height:20px;'>页 212 <input type='button' value='确定' id='jump_sure' style='width:40px;height:23px;' οnclick='jump();'>"; 213 214 return implode('', $nav) . $jump; 215 } 216 217 } 218 219 220 221 /** 222 * 创建分页类的工厂 223 * @param $type 表示分页样式 输入1,2,3 224 * return $obj 分页类的实例对象 225 */ 226 class page_factory 227 { 228 private static $obj; 229 public static function operation_page($type){ 230 try{ 231 $error = "Please input the number between 1 and 3 carefully"; 232 switch($type){ 233 case '1': 234 self::$obj = new page_css1(); 235 break; 236 case '2': 237 self::$obj = new page_css2(); 238 break; 239 case '3': 240 self::$obj = new page_css3(); 241 break; 242 default: 243 throw new Exception($error); 244 } 245 return self::$obj; 246 }catch (Exception $e) { 247 echo 'Caught exception: ', $e->getMessage(), "\n"; 248 exit; 249 } 250 } 251 } 252 253 //测试 254 $obj = page_factory::operation_page('3'); 255 $page = isset($_GET['page'])?$_GET['page']:1; 256 $obj->init_page(200,$page,10,6); 257 echo $obj->display(); 258 ?> 259 <style type="text/css"> 260 a{ 261 text-decoration: none; 262 } 263 </style> 264 265 <script type="text/javascript"> 266 function jump(){ 267 var jump_page_num = parseInt(document.getElementsByName('jump')[0].value); 268 var max_page = parseInt(document.getElementsByName('jump')[0].getAttribute('max_page')); 269 270 if(jump_page_num >= max_page){ 271 jump_page_num = max_page; 272 } 273 console.log(jump_page_num); 274 url= window.location.href; 275 276 var page_index = url.lastIndexOf('page'); 277 if(page_index == -1){ 278 window.location.href = url + '&page=' + jump_page_num; 279 } 280 window.location.href = url.replace(/page=[1-9][0-9]*/,'page='+jump_page_num); 281 } 282 </script>
在operation类中将其分页类的属性得到,然后利用3种样式类 page_css1() || page_css2() || page_css3() 来继承,然后通过后面的工厂来选择样式。
总的来说,思路很简单。
当然,我还有很多考虑不周的地方
在这里,我将所有的类的方法都写在这一个文件里,其实很不人性化,一般,在公司里,严格要求的话,要便于识别代码,且能清晰的认识代码,知道其意义
这里做的不是很明确。
在里面的测试部分,就很清晰的知道该文件时怎么操作的。