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