非常好用的Zend Framework分页类
程序员文章站
2023-12-04 08:20:58
在这里和大家分享一个非常好用的 zend framework 分页类
具体效果可见本站的分页效果, css样式可根据个人设计感进行更变。
...
在这里和大家分享一个非常好用的 zend framework 分页类
具体效果可见本站的分页效果, css样式可根据个人设计感进行更变。
这里我会举例演示如何使用该类, 如下:
indexcontroller.php, 在 action 中写入如下代码:
复制代码 代码如下:
protected $_curpage = 1; //默认第一页
const perpagenum = 4; //每页显示条目数
public function indexaction()
{
// $this->_blogmodel 已实例化 blog model
// $rows -> 获得到所展示数据的总条目数
$rows = $this->_blogmodel->gettotalrows();
if($pagenum = $this->getrequest()->getparam('page')) {
//如果有值传入,覆盖初始的第一页
$this->_curpage = $pagenum;
}
//把数据表中的数据传到前端
$this->view->bloginfo = $this->_blogmodel->getbloginfo(
self::perpagenum, ($this->_curpage-1)*self::perpagenum
);
//实例化分页类,并传到前端
$this->view->pagebar = $this->displaypagebar($rows);
}
private function displaypagebar($totalrows)
{
$pager = new zend_pagination($totalrows,self::perpagenum);
return $pager->getnavigation();
}
models/blog.php,写入如下代码:
复制代码 代码如下:
public function getbloginfo($perpagenum = null, $limit = null)
{
return $this->fetchall('1 = 1', 'blog_id desc', $perpagenum, $limit)
->toarray();
}
public function gettotalrows($where = '1=1')
{
return $this->fetchall($where)->count();
}
index.phtml, 写入如下代码:
复制代码 代码如下:
<div class="page">
<!--?php echo $this--->pagebar; ?>
</div>
到这里,就可以看见效果了, 如想追求更好的页面效果, 请根据个人喜好修改分页类,这里就不作详细示例
复制代码 代码如下:
class zend_pagination
{
private $_navigationitemcount = 6; //导航栏显示导航总页数
private $_pagesize = null; //每页项目数
private $_align = "right"; //导航栏显示位置
private $_itemcount = null; //总项目数
private $_pagecount = null; //总页数
private $_currentpage = null; //当前页
private $_front = null; //前端控制器
private $_pageparaname = "page"; //页面参数名称
private $_firstpagestring = "|<<"; //导航栏中第一页显示的字符
private $_nextpagestring = ">>"; //导航栏中前一页显示的字符
private $_previouspagestring = "<<"; //导航栏中后一页显示的字符
private $_lastpagestring = ">>|"; //导航栏中最后一页显示的字符
private $_splitstring = " | "; //页数字间的间隔符
public function __construct($itemcount, $pagesize)
{
if (!is_numeric($itemcount) || (!is_numeric($pagesize))) {
throw new exception("pagination error:not number");
}
$this->_itemcount = $itemcount;
$this->_pagesize = $pagesize;
$this->_front = zend_controller_front::getinstance();
$this->_pagecount = ceil($itemcount/$pagesize); //总页数
$page = $this->_front->getrequest()->getparam($this->_pageparaname);
if (empty($page) || (!is_numeric($page))) {
//为空或不是数字,设置当前页为1
$this->_currentpage = 1;
} else {
if ($page < 1) {
$page = 1;
}
if ($page > $this->_pagecount) {
$page = $this->_pagecount;
}
$this->_currentpage = $page;
}
}
public function getcurrentpage()
{
return $this->_currentpage;
}
public function getnavigation()
{
$navigation = '<div style="text-align:'.$this->_align.';" class="pagecss">';
//当前页处于第几栏分页
$pagecote = ceil($this->_currentpage / ($this->_navigationitemcount - 1)) - 1;
//总分页栏
$pagecotecount = ceil($this->_pagecount / ($this->_navigationitemcount - 1));
//分页栏中起始页
$pagestart = $pagecote * ($this->_navigationitemcount -1) + 1;
//分页栏中终止页
$pageend = $pagestart + $this->_navigationitemcount - 1;
if($this->_pagecount < $pageend) {
$pageend = $this->_pagecount;
}
$navigation .= "总共: {$this->_itemcount} 条 共 {$this->_pagecount} 页\n ";
if($pagecote > 0) { //首页导航
$navigation .= '<a href="'.$this->createhref(1)
." \"="">$this->_firstpagestring</a> ";
}
if($this->_currentpage != 1) { //上一页导航
$navigation .= '<a href="'.$this->createhref($this->_currentpage-1);
$navigation .= " \"="">$this->_previouspagestring</a> ";
}else{
$navigation .= $this->_previouspagestring . ' ';
}
while ($pagestart <= $pageend) //构造数字导航区
{
if ($pagestart == $this->_currentpage) {
$navigation .= "<b>$pagestart</b>" . $this->_splitstring;
} else {
$navigation .= '<a href="'.$this->createhref($pagestart)
." \"="">$pagestart</a>"
. $this->_splitstring;
}
$pagestart++;
}
if($this->_currentpage != $this->_pagecount) { //下一页导航
$navigation .= ' <a href="'
. $this->createhref($this->_currentpage+1)
. " \"="">$this->_nextpagestring</a> ";
}else{
$navigation .= $this->_nextpagestring;
}
if ($pagecote < $pagecotecount-1) { //未页导航
$navigation .= '<a href="'
. $this->createhref($this->_pagecount)
. " \"="">$this->_lastpagestring</a> ";
}
$navigation .= ' 到 <select onchange="window.location=\' '
. $this->createhref()
. '\'+this.options[this.selectedindex].value;">';
for ($i=1;$i<=$this->_pagecount;$i++){
if ($this->getcurrentpage()==$i){
$selected = "selected";
} else {
$selected = "";
}
$navigation .= '<option value=" . $i . " '="" .="" $selected="">'
. $i
. '</option>';
}
$navigation .= '</select>';
$navigation .= " 页</div>";
return $navigation;
}
public function getnavigationitemcount()
{
return $this->_navigationitemcount;
}
public function setnavigationitemcoun($navigationcount)
{
if(is_numeric($navigationcount)) {
$this->_navigationitemcount = $navigationcount;
}
}
public function setfirstpagestring($firstpagestring)
{
$this->_firstpagestring = $firstpagestring;
}
public function setpreviouspagestring($previouspagestring)
{
$this->_previouspagestring = $previouspagestring;
}
public function setnextpagestring($nextpagestring)
{
$this->_nextpagestring = $nextpagestring;
}
public function setlastpagestring($lastpagestring)
{
$this->_lastpagestring = $lastpagestring;
}
public function setalign($align)
{
$align = strtolower($align);
if ($align == "center") {
$this->_align = "center";
} elseif ($align == "right") {
$this->_align = "right";
} else {
$this->_align = "left";
}
}
public function setpageparamname($pageparamname)
{
$this->_pageparaname = $pageparamname;
}
public function getpageparamname()
{
return $this->_pageparaname;
}
private function createhref($targetpage = null)
{
$params = $this->_front->getrequest()->getparams();
$module = $params["module"];
$controller = $params["controller"];
$action = $params["action"];
$targeturl = $this->_front->getbaseurl()
. "/$module/$controller/$action";
foreach ($params as $key => $value)
{
if($key != "controller" && $key != "module"
&& $key != "action" && $key != $this->_pageparaname) {
$targeturl .= "/$key/$value";
}
}
if (isset($targetpage)) { //指定目标页
$targeturl .= "/$this->_pageparaname/$targetpage";
} else {
$targeturl .= "/$this->_pageparaname/";
}
return $targeturl;
}
}
这里再简单回顾下 mysql 中的 limit offset
假设数据库表 blog 存在 13 条数据。
语句1:select * from blog limit 9, 4
语句2:select * from blog limit 4 offset 9
//语句1和2均返回表 blog 的第 10、11、12、13 行
//语句1中的 9 表示从表的第十行开始, 返回 4 行
//语句2中的 4 表示返回 4 行,offset 9 表示从表的第十行开始
如下语句显示分页效果:
语句3:select * from blog limit ($this->_curpage - 1)* self::perpagenum, self::perpagenum;
语句4:select * from blog limit self::perpagenum offset ($this->_curpage - 1) * self::perpagenum;