'上页', "next"=>'下页', "first"=>'', "last"=>'');//在分页信息中显示内容,可以自己设置 public $listNum =10; //默认分页列表显示的个数 /** * 构造方法,可以设置分页类的属性 * @param int $total 计算分页的总记录数 * * @param int $page 第几页 * @param int $listRows 可选的,默认每页需要显示的记录数 * @param string $pa 可选的,为向目标页面传递参数 */ public function __construct($total,$page, $listRows=25, $pa=""){ $this->total = $total; $this->listRows = $listRows; $this->uri = $this->getUri($pa); $page = !empty($page) ? $page : 1; if($total > 0) { if(preg_match('/\D/', $page) ){ $this->page = 1; }else{ $this->page = $page; } }else{ $this->page = 0; } $this->pageNum = ceil($this->total/$this->listRows); $this->limit = $this->setLimit(); } /** * 用于设置显示分页的信息,可以连贯操作 * @param string $param 是数组config的下标 * @param string $value 用于设置config下标对应的元素值 * @return object 返回本对象自己$this */ function set($param, $value){ if(array_key_exists($param, $this->config)){ $this->config[$param]=$value; } return $this; } public function setLimit(){ if($this->page > 0) return ($this->page-1)*$this->listRows.", {$this->listRows}"; else return 0; } public function getUri($param){ $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param; $parse = parse_url($url); if (isset($parse["query"])) { parse_str($parse["query"], $params); unset($params["page"]); $url = $parse["path"] . "?" . http_build_query($params); return $url; } else { return $url; } } public function __get($args){ if($args=="limit") return $this->limit; else return null; } public function start(){ if($this->total==0) return 0; else return ($this->page-1)*$this->listRows+1; } public function end(){ return min($this->page*$this->listRows,$this->total); } public function firstprev(){ if($this->page > 1) { $str ="
  • {$this->config["first"]}
  • "; $str .="
  • {$this->config["prev"]}
  • "; return $str; }else{ $str ="
  • {$this->config["first"]}
  • "; $str .="
  • {$this->config["prev"]}
  • "; return $str; } } public function pageList(){ $linkPage = ""; $inum = 2; for($i=$inum; $i>=1; $i--){ $page = $this->page-$i; if($page>=1) $linkPage .="
  • {$page}
  • "; } if($this->pageNum > 1) $linkPage .='
  • '.$this->page.'
  • '; for($i=1; $i<=$inum; $i++){ $page = $this->page+$i; if($page<=$this->pageNum) $linkPage .= "
  • {$page}
  • "; else break; } return $linkPage; } public function nextlast(){ if($this->page != $this->pageNum) { $str ="
  • {$this->config["next"]}
  • "; $str .="
  • {$this->config["last"]}
  • "; return $str; }else{ $str ="
  • {$this->config["next"]}
  • "; $str .="
  • {$this->config["last"]}
  • "; return $str; } } /** * 按指定的格式输出分页 */ function fpage(){ if($this->total <= $this->listRows){ return; } $fpage = ''; return $fpage; } }