欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 自己写的好看的分页类

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

  1 pageSize = $pageSize; 19         $this->showPage = $showPage; 20         $this->count    = $count; 21         $this->page     = $this->checkPage($currentPage); 22         $this->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 .= "";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 }

上面是类文件的所有代码,以下是调用实例:

";echo "
follow_trade_id
follow_id
";while($rs = mysql_fetch_assoc($query)) {    echo "
{$rs['follow_trade_id']}
{$rs['follow_id']}
";}echo "";//读取分页条$p->GetPagerContent();

样式CSS:

效果如图所示: