Page分页类(二)
在经过我老大的提醒下,我将代码进行了规整,并且分了文件
我先写了一些基础的css_factory.class.php文件,得到分页类的基本属性,然后新建一个css文件夹,里面就是包括css的样式文件,即:page_css1.class.php + page_css2.class.php + page_css3.class.php 文件,这3个文件通过继承css_factory.class.php,然后再css_factory.class.php同级页面上写一个page.class.php,里面包括operation方法,用来选择样式,得到实例对象
做调整的部分,就是加了一个单例模式,因为在一个网站中肯定有很多的要展示分页的地方,所以要写一个单例,表示当前的对象是唯一的。
css_factory.class.php:
1 <?php 2 3 /** 4 * class css_factory 5 * 6 * Factory Method使用一个类的实例化延迟到其子类 7 */ 8 9 10 class css_factory 11 { 12 13 /** 14 * total 数据的总条数 15 */ 16 protected $total = 1; 17 18 /** 19 * per_page_num 每页显示的数据条数 20 */ 21 protected $per_page_num = 3; 22 23 /** 24 * page 当前页码 25 */ 26 protected $page=1; 27 28 /** 29 * url 当前的链接地址 30 */ 31 protected $url = ''; 32 33 /** 34 * total_page 总页数 35 */ 36 protected $total_page = 0; //总页数 37 38 /** 39 * init_page_var Array 初始化分页类的属性数组 40 */ 41 //private $init_page_var = array(); 42 43 /** 44 * init_css()方法 初始化分页类的数据 45 */ 46 //public function init_css($init_page_var=false) 47 public function init_css($total,$page=false,$per_page_num=false) 48 { 49 /* 50 $this->total=isset($init_page_var['total']) ? $init_page_var['total'] : $this->total ; 51 $this->per_page_num=isset($init_page_var['per_page_num']) ? $init_page_var['per_page_num'] : $this->per_page_num ; 52 //echo $this->per_page_num,'~~~~~'; 53 */ 54 55 $this->total = intval($total); 56 if($per_page_num){ 57 $this->per_page_num = intval($per_page_num); 58 } 59 if($page){ 60 $this->page = intval($page); 61 } 62 63 64 //总页数 65 $total_page = ceil($this->total/$this->per_page_num); 66 67 //地址栏的参数保存 68 $uri = $_SERVER['REQUEST_URI']; 69 $parse = parse_url($uri); 70 71 $param = array(); 72 if(isset($parse['query'])){ 73 parse_str($parse['query'],$param); 74 } 75 //不管有没有都unset掉$param下的page单元,因为page是要算出来的 76 unset($param['page']); 77 78 //这么是为了将param中的page去掉 79 80 $url = $parse['path'] . '?'; 81 if(!empty($param)){ 82 $param = http_build_query($param);//得到去掉page的参数 83 $url = $url . $param . '&';//得到完整路径 84 } 85 86 $this->total_page = $total_page; 87 $this->url = $url; 88 } 89 90 91 }
1 <?php 2 3 /** 4 * class page_css1 5 * 6 * 显示第一种样式 7 */ 8 9 class page_css1 extends css_factory 10 { 11 12 /** 13 * display() 展示分页功能 14 * return String 返回展示语句 15 */ 16 public function display(){ 17 18 /* 19 * $nav array 页码导航 20 */ 21 $nav = array(); 22 23 $nav[] = "<span>" . $this->page . '</span>'; 24 25 /*在这里 count($nav)不能放在for语句中的第一个条件里,因为这在for循环语句中是进行初始化用的,而在这里$nav还没有元素,即,还没有增进元素,写在前面后面的条件就不能起作用*/ 26 27 for($left = $this->page-1,$right = $this->page+1;($left>=1||$right<=$this->total_page) && count($nav)<$this->display_page_num;){ 28 29 if($left>=1){ 30 array_unshift($nav,'<a href="' . $this->url . 'page=' .$left . '">[' . $left . ']</a>'); 31 $left-=1; 32 } 33 if($right<=$this->total_page){ 34 array_push($nav, '<a href="' . $this->url . 'page=' . $right . '">[' . $right . ']</a>'); 35 $right+=1; 36 } 37 38 } 39 40 /** 41 * 将首页 末页 上下页 的链接页码放入$nav数组中 42 */ 43 $pageprev = ($this->page-1>=1)? ($this->page-1):1; 44 $pagenext = ($this->page+1<=$this->total_page)? ($this->page+1):$this->total_page; 45 46 array_unshift($nav,'<a href="' . $this->url . 'page=1">[首页]</a> 47 <a href="' . $this->url . 'page='. $pageprev .'">[上一页]</a>'); 48 array_push($nav, '<a href="' . $this->url . 'page=' . $pagenext . '">[下一页]</a> 49 <a href="' . $this->url . 'page=' . $this->total_page . '">[末页]</a>'); 50 51 echo implode('', $nav); 52 } 53 }
page_css2.class.php:
1 <?php 2 3 /** 4 * class page_css2 5 * 6 * 显示第二种样式 7 */ 8 9 class page_css2 extends css_factory 10 { 11 12 /** 13 * display() 展示分页功能 14 * return String 返回展示语句 15 */ 16 public function display(){ 17 18 /* 19 * $nav array 页码导航 20 */ 21 $nav = array(); 22 23 $nav[] = "<span style='font-weight:bold;'><strong>" . $this->page . '</strong></span>'; 24 25 for($left = $this->page-1,$right = $this->page+1;($left>=1||$right<=$this->total_page) && count($nav)<$this->display_page_num;){ 26 27 if($left>=1){ 28 array_unshift($nav,'<a href="' . $this->url . 'page=' .$left . '">' . " " . $left . " " . '</a>'); 29 $left-=1; 30 } 31 if($right<=$this->total_page){ 32 array_push($nav, '<a href="' . $this->url . 'page=' . $right . '">' . " " . $right . " " .'</a>'); 33 $right+=1; 34 } 35 36 } 37 38 /** 39 * 将首页 末页 上下页 的链接页码放入$nav数组中 40 */ 41 $pageprev = ($this->page-1>=1)? ($this->page-1):1; 42 $pagenext = ($this->page+1<=$this->total_page)? ($this->page+1):$this->total_page; 43 array_unshift($nav,'<a href="' . $this->url . 'page='. $pageprev .'">'. "<" . '</a>'); 44 array_unshift($nav,'<a href="' . $this->url . 'page=1">'. "<<" .'</a>' . " "); 45 array_push($nav, '<a href="' . $this->url . 'page=' . $pagenext . '">'.">".'</a>' . " "); 46 array_push($nav, '<a href="' . $this->url . 'page=' . $this->total_page . '">'. ">>" .'</a>'); 47 48 //页码列表 49 $selector=<<<PAGE 50 <select id="pages" onchange="window.location.href='$this->url'+'page='+this.value" > 51 PAGE; 52 53 /** 54 * $selector 循环页码 显示在一个下拉菜单中 55 */ 56 for($p=1;$p<=$this->total_page;$p++){ 57 if($this->page==$p){ 58 $selector .= <<<PAGE 59 <option value="$p" selected>$p</option> 60 PAGE; 61 }else{ 62 $selector .=<<<PAGE 63 <option value="$p">$p</option> 64 PAGE; 65 } 66 } 67 68 $selector .='</select>'; 69 70 echo implode('', $nav) . $selector; 71 } 72 }
page_css3.class.php:
1 <?php 2 3 /** 4 * class page_css3 5 * 6 * 显示第三种样式 7 */ 8 9 class page_css3 extends css_factory 10 { 11 12 /** 13 * display() 展示分页功能 14 * return String 返回展示语句 15 */ 16 public function display(){ 17 18 /* 19 * $nav array 页码导航 20 */ 21 $nav = array(); 22 23 /** 24 *显示分页信息 放入$nav数组中 25 */ 26 $info = <<<PAGE 27 总计<span id="total">$this->total</span>记录, 28 分为<span id="">$this->total_page</span>页, 29 当前第<span id="">$this->page</span>页, 30 每页<span id="">$this->per_page_num</span>条记录 31 PAGE; 32 33 $nav[] = "<span style='color:#f00;'><strong>" . $this->page . '</strong></span>'; 34 35 for($left = $this->page-1,$right = $this->page+1;($left>=1||$right<=$this->total_page) && count($nav)<$this->display_page_num;){ 36 37 if($left>=1){ 38 array_unshift($nav,'<a href="' . $this->url . 'page=' .$left . '">' . " " . $left . " " . '</a>'); 39 $left-=1; 40 } 41 if($right<=$this->total_page){ 42 array_push($nav, '<a href="' . $this->url . 'page=' . $right . '">' . " " . $right . " " .'</a>'); 43 $right+=1; 44 } 45 } 46 /** 47 * 将首页 末页 上下页 的链接页码放入$nav数组中 48 */ 49 $pageprev = ($this->page-1>=1)? ($this->page-1):1; 50 $pagenext = ($this->page+1<=$this->total_page)? ($this->page+1):$this->total_page; 51 array_unshift($nav,'<a href="' . $this->url . 'page='. $pageprev .'">上一页</a>'); 52 array_unshift($nav,'<a href="' . $this->url . 'page=1">首页</a>' . " "); 53 array_push($nav, '<a href="' . $this->url . 'page=' . $pagenext . '">下一页</a>' . " "); 54 array_push($nav, '<a href="' . $this->url . 'page=' . $this->total_page . '">尾页</a>'); 55 array_unshift($nav, "$info"); 56 57 $jump = " 跳到<input type='text' name='jump' max_page=" .$this->total_page . " style='width:30px;height:20px;'>页 58 <input type='button' value='确定' id='jump_sure' style='width:40px;height:23px;' onclick='jump();'>"; 59 60 echo implode('', $nav) . $jump; 61 } 62 }
page.class.php:
1 <?php 2 3 /** 4 * 分页类的实现 5 * page.class.php 6 * 单例模式 7 */ 8 9 require('./css_factory.class.php'); 10 11 require('./css/page_css1.class.php'); 12 require('./css/page_css2.class.php'); 13 require('./css/page_css3.class.php'); 14 15 16 17 class operation 18 { 19 /** 20 * 单例对象 $instance 21 */ 22 private static $instance = null; 23 24 /** 25 * getInstance()方法 获得单例对象 26 */ 27 public static function getInstance() 28 { 29 if(!(self::$instance instanceof self)) 30 { 31 self::$instance = new self; 32 } 33 return self::$instance; 34 } 35 36 /** 37 * 分页样式的实例对象 $obj 38 */ 39 private static $obj; 40 41 /** 42 * operation_page() 选择样式 操作分页类 43 * @param type int 选择的第几个的样式 44 * return css 实例对象 45 */ 46 public static function operation_page($type){ 47 try{ 48 $error = "Please input the number between 1 and 3 carefully"; 49 switch($type){ 50 case '1': 51 self::$obj = new page_css1(); 52 break; 53 case '2': 54 self::$obj = new page_css2(); 55 break; 56 case '3': 57 self::$obj = new page_css3(); 58 break; 59 default: 60 throw new Exception($error); 61 } 62 return self::$obj; 63 }catch (Exception $e) { 64 echo 'Caught exception: ', $e->getMessage(), "\n"; 65 return; 66 } 67 } 68 69 } 70
//测试
$oper = operation::getInstance();
$obj = $oper::operation_page('1');
$page = isset($_GET['page'])?$_GET['page']:1;
$init_arr = array('total'=>20);
$obj->init_css($init_arr);
$obj->display();
71 ?>
72 <style type="text/css"> 73 a{ 74 text-decoration: none; 75 } 76 </style> 77 78 <script type="text/javascript"> 79 80 /** 81 * jump() 跳转函数 82 * 83 */ 84 function jump(){ 85 var jumpPageNum = parseInt(document.getElementsByName('jump')[0].value); 86 var maxPage = parseInt(document.getElementsByName('jump')[0].getAttribute('max_page')); 87 88 /*如果输入的不是数字,默认是1*/ 89 if(isNaN(jumpPageNum)){ 90 jumpPageNum = 1; 91 } 92 93 /*若输入的数字大于最大页码,默认跳转到最大页码*/ 94 if(jumpPageNum >= maxPage){ 95 jumpPageNum = maxPage; 96 } 97 98 url= window.location.href; 99 100 var querySymbolIndex = url.lastIndexOf('?'); 101 102 if(querySymbolIndex == -1){ 103 //若没有 ? 号 则代表url上没有参数,则需要加上?page=jumpPageNum 104 window.location.href = url + '?page=' + jumpPageNum; 105 } 106 else{ 107 //有?号 则代表URL上有参数,则需先验证后面是否有page参数 108 var pageIndex = url.indexOf('page',querySymbolIndex); 109 110 if(pageIndex == -1){ 111 //没有page参数,需要加上 &page=jumpPageNum 112 window.location.href = url + '&page=' + jumpPageNum; 113 }else{ 114 //有page参数 则要替换page参数,将其值改为jumpPageNum 115 window.location.href = url.replace(/page=[1-9][0-9]*/,'page='+jumpPageNum); 116 } 117 118 } 119 120 } 121 </script>
在page.class.php中展示了测试代码,即可根据选择的样式来显示。
在这里js部分也有考虑的全面些,但还有不全面的地方,请看(三)。