• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 自己写的好看的分页类

php 搞代码 4年前 (2022-01-23) 13次浏览 已收录 0个评论

自己写的一个分页类 ,不是很完整,个别没有做验证,但可以使用,分页效果见文章底部。除了链接数据库的代码没有粘贴上来,其他的都粘贴了。供学习使用~

  1 <?php  2 /**  3  * Created by PhpStorm.  4  * User: Caoxt  5  * Date: 15-7-3  6  * Time: 上午10:03  7  */  8 class page {  9     public $pageSize;  //每页显示的文章条数 10     public $showPage;  //页数条显示的条数 11     public $count;     //文章总条数 12     public $page;      //当前页 13     public $mergyStr; //存储页码的变量 14     public $skip;    //跳转页开关,默认为false关闭 15  16     //初始化参数 17     public function __construct($count, $pageSize = 10, $showPage = 5, $currentPage = 1, $skip = false) { 18         $this->pageSize = $pageSize; 19         $this->showPage = $showPage; 20         $this->count    = $count; 21         $this->page     = $this->checkPage($currentPage); 22         $this-><strong>2本文来源gaodaima#com搞(代@码$网6</strong><pre>搞gaodaima代码

mergyStr = ”; 23 $this->skip = $skip; 24 } 25 26 //检查传递的$page的合法性 27 public function checkPage($currentPage) { 28 if($currentPage $this->totalPages()) { 32 $currentPage = $this->totalPages(); 33 } 34 return $currentPage; 35 } 36 37 //计算偏移量 38 public function pageOffset() { 39 return ($this->showPage-1)/2; 40 } 41 42 //计算总页数 43 public function totalPages() { 44 return ceil($this->count/$this->pageSize); 45 } 46 47 //获取页面URL 48 public function getPageUrl() { 49 $CurrentUrl = $_SERVER[“REQUEST_URI”]; 50 $arrUrl = parse_url($CurrentUrl); 51 $urlQuery = $arrUrl[“query”]; 52 53 if($urlQuery){ 54 //print_r($this->page); 55 $urlQuery = preg_replace(“/(^|&)page=/” . $this->page, “”, $urlQuery); 56 $CurrentUrl = str_replace($arrUrl[“query”], $urlQuery, $CurrentUrl); 57 58 if($urlQuery){ 59 $CurrentUrl.=”&page”; 60 } 61 else $CurrentUrl.=”page”; 62 63 } else { 64 $CurrentUrl.=”?page”; 65 } 66 67 return $CurrentUrl; 68 } 69 70 //页码显示行 71 public function GetPagerContent() { 72 $start = 1; 73 $end = $this->totalPages(); 74 $this->mergyStr .= “

“; 75 if($this->page > 1) { 76 $this->mergyStr .= ” getPageUrl().”=”.”1′>首页”; 77 $this->mergyStr .= ” getPageUrl().”=”.($this->page-1).”‘>上一页”; 78 }else{ 79 $this->mergyStr .= ” 首页“; 80 $this->mergyStr .= ” 上一页“; 81 } 82 83 if($this->totalPages() > $this->showPage) { 84 if($this->page > $this->pageoffset()+1) { 85 $this->mergyStr .= “…”; 86 } 87 88 if($this->page > $this->pageoffset()) { 89 $start = $this->page-$this->pageoffset(); 90 $end = $this->totalPages() > $this->page+$this->pageoffset() ? $this->page+$this->pageoffset() : $this->totalPages(); 91 } 92 else{ 93 $start = 1; 94 $end = $this->totalPages() > $this->showPage ? $this->showPage : $this->totalPages(); 95 } 96 97 if($this->page + $this->pageoffset() > $this->totalPages()) { 98 $start = $start – ($this->page + $this->pageoffset() – $end); 99 }100 101 }102 103 for($i=$start; $ipage) {105 $this->mergyStr .= “{$i}“;106 }else{107 $this->mergyStr .= ” getPageUrl().”=”.$i.”‘>{$i}”;108 }109 110 }111 112 if($this->totalPages() > $this->showPage && $this->totalPages() > $this->page + $this->pageoffset()) {113 $this->mergyStr .= “…”;114 }115 116 if($this->page totalPages()) {117 $this->mergyStr .= ” getPageUrl().”=”.($this->page+1).”‘>下一页”;118 $this->mergyStr .= ” getPageUrl().”=”.$this->totalPages().”‘>尾页”;119 }else{120 $this->mergyStr .= ” 下一页“;121 $this->mergyStr .= ” 尾页“;122 }123 124 $this->mergyStr .= ” 共{$this->totalPages()}页”;125 126 //显示跳转框127 if($this->skip == true) {128 $this->mergyStr .= $this->skipNumPage();129 }130 131 $this->mergyStr .= “

“;132 133 print_r($this->mergyStr);134 }135 136 //跳转页137 public function skipNumPage() {138 $this->mergyStr .= “”;139 $this->mergyStr .= “第页”;140 $this->mergyStr .= “”;141 $this->mergyStr .= “”;142 }143 144 }


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 自己写的好看的分页类
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址