<?php<br />
/*<br />
* 效果是百度分页那样的,不涉及数据库操作,<br />
* 实际上,分页的确是和数据库操作分开的<br />
* 使用方法<br />
* $total = 50;<br />
* $page = new Page($total);<br />
* echo $page->show();<br />
*<br />
*/<br />
class Page {<br />
public $total; //总页数,在这里,你需要把总页数算出来<br />
public $p; //当前页数<br />
public $start; //排序起始页<br />
public $end; //排序终止页<br />
public $url; //当前的url,例如 index.php?p=<br />
public $display; //左边显示的页数,例如是5,就是说左边有5页,总共是10页<br />
public function __construct($total, $url = ?p=, $display = 5) {<br />
$this->total = $total;<br />
$this->url = $url;<br />
$this->display = $display;<br />
$this->init();<br />
$this->order();<br />
}<br />
<br />
/**<br />
*初始化,包括简单的安全处理,当然,你可以扩展这个函数以达到你的要求<br />
*/<br />
public function init() {<br />
//获取当前的页数<br />
$this->p = (@$_GET[p] + 0 <= 0)? 1: (@$_GET[p] + 0);<br />
if (!is_int($this->p)) {<br />
$this->p = 1;<br />
}<br />
//如果有人在页面上输入一个较大的数,我是这样处理的,显示最后一页<br />
//你可以自己扩展为"当前没有你找到的页",把下面的去掉,在show函数里加个判断就行了<br />
if ($this->p >= $this->total) {<br />
$this->p = $this->total;<br />
}<br />
} <br />
/**<br />
*这里将是怎么显示为百度分页的那种效果,当然,已经够用了<br />
*还有局部没有处理好,如果处理好麻烦告诉我<br />
*/<br />
public function order() {<br />
if ($this->total <= 2 * $this->display) {<br />
$this->start = 1;<br />
$this->end = $this->total;<br />
} else {<br />
if ($this->p <= $this->display) {<br />
$this->start = 1;<br />
$this->end = 2 * $this->display;<br />
} else {<br />
if ($this->p > $this->display && ($this->total - $this->p >= $this->display - 1)) {<br />
$this->start = $this->p - $this->display;<br />
$this->end = $this->p + $this->display - 1;<br />
} else {<br />
$this->start = $this->total - 2 * $this->display + 1;<br />
$this->end = $this->total;<br />
}<br />
}<br />
}<br />
}<br />
public function show() {<br />
//如果没有数据,当然也就没有分页了<br />
if ($this->total <= 1) {<br />
return false;<br />
//exit;<br />
}<br />
else<br />
{<br />
$re = ; <br />
// $pre前一页 $next 后一页<br />
// $re .= "<a href=\"{$this->url}1\">首页</a>";<br />
$pre = ($this->p - 1 <= 0) ? 1 : ($this->p - 1);<br />
$re .= "<a href=\"{$this->url}$pre\">前一页</a>&nbsp;";<br />
//如果当前页是第一页,不要首页和前一页<br />
if ($this->p == 1) {<br />
$re = ;<br />
}<br />
for ($i = $this->start; $i <= $this->end; $i++) {<br />
$re .= ($i == $this->p)? "$i ": "<a href=\"{$this->url}$i\">[$i]</a>&nbsp;";<br />
}<br />
<br />
$next = ($this->p + 1 >= $this->total) ? $this->total : ($this->p + 1);<br />
//当前页是最后一页的页数,不要下一页和最后一页<br />
if ($this->p != $this->total) {<br />
$re .= "<a href=\"{$this->url}$next\">下一页</a>";<br />
// $re .= "<a href=\"{$this->url}$this->total\">最后一页</a>";<br />
} <br />
return $re;<br />
}<br />
}<br />
}<br />
?>