一个简单的PHP分页类
2015-09-08 15:54
861 查看
<?php /** * $pager = new cls_pager($total,$pernum); * $limit = $pager->getlimit();//获得limit 语句 * $pagerhtml = $pager->gethtml();//获得分页效果 * */ class module_pager2 { private $total;//总条数 private $pagetotal;//总页数 private $curpage;//当前页 private $pagenum;//每页数 private $limit;//limit 语句 private $nextpage; private $prepage; private $url=''; public function module_pager2($total,$pernum=20) { $this->total = $total; $this->pagenum = $pernum; $this->pagetotal = ($total>0)? ceil($this->total / $this->pagenum): 1; $this->curpage = (isset($_GET['p']) && ($_GET['p'] > 0)) ? intval($_GET['p']) : 1; $this->curpage = ($this->curpage > $this->pagetotal) ? $this->pagetotal : $this->curpage; $this->nextpage = ($this->curpage == $this->pagetotal) ? '' : ($this->curpage + 1); $this->prepage = ($this->curpage == 1) ? '' : ($this->curpage - 1); } public function getlimit() { $start = ($this->curpage - 1) * $this->pagenum; $str = " limit $start,$this->pagenum"; return $str; } /** * 计算limit值 */ public function get_limit() { $start = ($this->curpage - 1) * $this->pagenum; return " $start,$this->pagenum"; } public function geturl() { if($this->url==''){ $this->url = 'index.php?'. http_build_query( $_GET ); } $this->url = preg_replace('/[?,&]p=(\w)+/','',$this->url); $this->url .= (strpos($this->url,"?") === false) ? '?' : '&'; return $this->url; } public function seturl($url){ $this->url=$url; } public function gethtml1() { $s = $this->curpage - 5; $i = ($s > 0) ? $s : 1; $e = $this->curpage + 5; $end = ($e < $this->pagetotal) ? $e : $this->pagetotal; $html = '<div class="pageItem clearfix fr"><div class="fl mr10">'; $url = $this->geturl(); if($this->curpage > 5) { $html .= "<a href=".$url."p=1>首页</a> "; } if($this->prepage) { $html .= "<a href=".$url."p=".$this->prepage.">上一页</a> "; } for($i;$i <= $end; $i++) { if($i == $this->curpage) { $html .= "<em>".$i . "</em> "; //$html .= "<a href='javascript:void(0)' class='cur'>".$i . "</a>"; } else { $html .= "<a href=".$url."p=$i>$i</a> "; } } if($this->nextpage && ($this->nextpage <= $this->pagetotal)) { $html .= "<a href=".$url."p=".$this->nextpage.">下一页</a> "; } if(($this->curpage + 5) < $this->pagetotal) { $html .= "<a href=".$url."p=".$this->pagetotal.">末页</a> "; } $html .= '</div><div class="fl mr10">到第<input type="text" id="changepage" onkeydown="if(event.keyCode==13) {window.location=\''.$url.'p=\'+this.value; return false;}">页</div><a href="javascript:;" onclick="window.location=\''.$url.'p=\'+document.getElementById(\'changepage\').value; return false;" class="fl btn02">确定</a></div>'; //$html .= " 共 $this->total 个 $this->pagetotal 页"; return $html; } /** * * @return type */ public function gethtml() { $s = $this->curpage - 5; $i = ($s > 0) ? $s : 1; $e = $this->curpage + 5; $end = ($e < $this->pagetotal) ? $e : $this->pagetotal; $html = ''; $url = $this->geturl(); if($this->curpage > 5) { $html .= "<a class=\"up\" href=".$url."p=1>首页</a> "; } if($this->prepage) { $html .= "<a class=\"up\" href=".$url."p=".$this->prepage.">上一页</a> "; } for($i;$i <= $end; $i++) { if($i == $this->curpage) { $html .= '<span>' . $i . '</span>'; } else { $html .= "<a class=\"\" href=".$url."p=$i>$i</a> "; } } if($this->nextpage && ($this->nextpage <= $this->pagetotal)) { $html .= "<a class=\"down\" href=".$url."p=".$this->nextpage.">下一页</a> "; } if(($this->curpage + 5) < $this->pagetotal) { $html .= "<a class=\"down\" class=\"down\" href=".$url."p=".$this->pagetotal.">末页</a> "; } //$html .= "共 $this->total 个 $this->pagetotal 页"; return $html; } /** * 用于当分页需要ajax时传递javascript函数,获得交易记录时用到 * author:Riven<jiangtao1@leju.sina.com.cn> * date:2011-07-09 * @return type */ public function get_pagination_html($jump_js = null, $cut_line = '', $current_class ='current') { $s = $this->curpage - 5; $i = ($s > 0) ? $s : 1; $e = $this->curpage + 5; $end = ($e < $this->pagetotal) ? $e : $this->pagetotal; $html = ''; $url = $this->geturl(); if($this->pagetotal) { $html .= '<span class="gray">共' . $this->pagetotal . '页</span>'; } if(isset($_SERVER['HTTP_HOST'])) { $base_url = 'http://' . $_SERVER['HTTP_HOST'] . '/'; } else { $base_url = 'http://www.eju.com/'; } $url = $base_url . $url; if($jump_js) { $url = 'javascript:' . $jump_js . '(\'' . $url . 'p=' . '%s' . '\');'; } else { $url .= 'p=' . '%s'; } if($this->prepage) { $html .= '<a href="' . sprintf($url, $this->prepage) . '" class="prev"><</a>' . $cut_line; } if($end != 1) { if($i != 1) { if($i == 2) { $html .= '<a href="'.sprintf($url, 1) . '" onclick="return false" >' . 1 . '</a>'; } else { $html .= '<a href="'.sprintf($url, 1) . '" onclick="return false" >' . 1 . '</a>…'; } } for($i;$i <= $end; $i++) { if($i == $this->curpage) { $html .= '<a href="#" onclick="return false" class="'.$current_class.'" >' . $i . '</a>' . $cut_line; } else { $html .= '<a href="'.sprintf($url, $i) . '">'.$i.'</a>' . $cut_line; } } if($this->pagetotal != $end) { if($this->pagetotal -1 == $end) { $html .= '<a href="'.sprintf($url, $this->pagetotal) . '" onclick="return false" >' . $this->pagetotal . '</a>'; } else { $html .= '<a href="'.sprintf($url, $this->pagetotal) . '" onclick="return false" >' . $this->pagetotal . '</a>…'; } } } if($this->nextpage && ($this->nextpage <= $this->pagetotal)) { $html .= '<a href="'.sprintf($url, $this->nextpage) . '" class="next">></a>'; } $html = trim($html, $cut_line); return $html; } }
调用方法:<pre name="code" class="php"> $pager = new module_pager2( $total, $page_size);$page_html = $pager->gethtml();$page_url = $pager->geturl();
并将对应变量传递给前端页面:
<pre name="code" class="php"> $this->_view->assign('pager', $page);$this->_view->assign('page_html', $page_html);$this->_view->assign('page_url', $page_url);$this->_view->assign('total', $total);前端简单调用,这里是smarty模板的写法<div class="pageLine clearfix"><div class="fl pt5 pl5">共计<!--{$list_count}-->条记录</div><div class="pageItem clearfix fr"><div class="fl mr10"><!--{$page_html}--></div></div></div>然后写个最简单的样式:<style>.pageItem a{padding: 9px;border: 1px solid #E4DDDD;}.pageItem span{padding: 9px;border: 1px solid #E4DDDD;margin-right: 15px;background-color: rgb(107, 206, 115);}.pageItem > div{float:none;}</style>最后效果图:整体都很简单,在此做下笔记
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- more、less 和 most 的区别
- 十万条Access数据表分页的两个解决方法
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查询计划】
- apache 环境下 php 的配置注意事项
- 高效的mysql分页方法及原理