一个简单的PHP分页类,吐槽版

<?php

class page{
 public $page;
 public $pagenum;
 public $pagesize;
 public function __construct($count,$pagesize){
  $this->pagenum=ceil($count/$pagesize);
  $this->pagesize=$pagesize;
  $this->page=(isset($_GET['p'])&&$_GET['p']>0)?intval($_GET['p']):1;//首次调用类么?是的话就让P值为1
 }

 public function getUrl(){
  $url =basename($_SERVER['PHP_SELF']). '?'.http_build_query($_GET);

  $url=preg_replace('/[?,&]p=(\w)+/','',$url);

   $url.=(strpos($url,"?")===false)?'?':'&';
  return $url;//这里返回的是没有P参数的URL
 }

 public function getPage(){
  $url=$this->getUrl();
  $start=$this->page-5;
  $start=$start>0?$start:1;
  $end=$start+9;
  $end=$end<$this->pagenum?$end:$this->pagenum;
  $pagestr='';
  if($this->page>5){
   $pagestr="<a href=".$url."p=1".">first page</a>";
  }
  if($this->page!=1){
   $pagestr="<a href=".$url."p=".($this->page-1).">pre  </a>";
  }
  for($i=$start;$i<=$end;$i++){
   $pagestr.="<a href=".$url."p=".$i."> ".$i." </a>";
  }
  if($this->page!=$this->pagenum){
   $pagestr.="<a href=".$url."p=".($this->page+1).">next </a>";
  }
  if($this->page+5<$this->pagenum){
   $pagestr.="<a href=".$url."p=".$this->pagenum.">last_page</a>";
  }
  return $pagestr;
 }
}

$page=new page(1000,7);
$str=$page->getPage();
echo $str;
?>

原文连接:http://www.oschina.net/code/snippet_182375_6242

你可能感兴趣的:(分页)