说实话 要不是今天看了一个网友问Zend_Paginator分页的问题 我还一直不知道 tukiz02
以前都是用自己写的一个PPH分页
为了解决这个网友的问题 亲自看了下手册 试了下 还不错 给弄出来了
现学现卖 是我的特点 马上发出来 给大家分享 tukiz05
不过事先说好 是不是最佳的方法 这个我不保证
代码写的简单与否 我也不保证
谁让我是个菜鸟呢!tukiz10
看看效果 (没加CSS)
代码开始
================
效果实现主要有三个文件
用于View的 ——
r.phtml (最终用户看到的结果列表页面)
pagelist.phtml(相当于是分页的一个模板。这里用不用新建一个文件我还没摸清楚 哈哈!)
SearchController.php (用于分页控制的文件)
r.phtml
[php]
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php if (count($this->paginator)): ?>
<?php foreach ($this->paginator as $item): ?>
<ul>
<?php echo "<li>" . $item['name'] . "</li>"; ?>
</ul>
<?php endforeach; ?>
<?php endif; ?>
/*
*上面部分显示每页的结果,这里调用的公司名*
*可根据实际要求替换格式*
*/
<?= $this->paginationControl($this->paginator, 'Elastic', 'search/pagelist.phtml'); ?>
/*这里就是显示分页的语句*/
[/php]
前面声明编码是因为从库里读出来的是中文字符(似乎说了废话)
pagelist.phtml
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="/search/r?page=<?php echo $this->previous; ?>">< 上一页</a> |
<?php else: ?>
<span class="disabled">< 上一页</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="/search/r?page=<?php echo $page; ?>"><?= $page; ?></a> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="/search/r?page=<?php echo $this->next; ?>">下一页 ></a>
<?php else: ?>
<span class="disabled">下一页 ></span>
<?php endif; ?>
</div>
<?php endif; ?>
pagelist.phtml实际上就是一个如何显示分页的模板,用的时候个人可以再自己改,注意为了实现分页效果,我已经改了链接的地址
SearchController.php
[php]require_once('Zend/Paginator.php');
require_once('Zend/Paginator/Adapter/Array.php');
class SearchController extends Zend_Controller_Action
{
public function init()
{
$this->initView();
}
public function rAction()
{
$page =1;
$numPerPage = 10;
if(isset($_GET['page']) && is_numeric($_GET['page'])){
$page = $_GET['page'];
}
$offset = $numPerPage*$page;
$db = Zend_Registry::get('db');
$select = $db->select()
->from('company' , '*')
->limit($offset , $numPerPage)
->order('pkid DESC');
$stmt = $select->query();
$results = $stmt->fetchall();
$count = count($results);
$this->view->count = $count;
$this->page($page,$numPerPage);
$this->render();
}
public function page($page,$numPerPage)
{
$db = Zend_Registry::get('db');
$select = $db->select()
->from('company' , '*')
->limit(200);
$stmt = $select->query();
$array = $stmt->fetchall();
$paginator = Zend_Paginator::factory($array);
$paginator->setCurrentPageNumber($page)
->setItemCountPerPage($numPerPage);
$this->view->paginator = $paginator;
}
public function pagelistAction()
{
Zend_Paginator::setDefaultScrollingStyle('Elastic');
Zend_View_Helper_PaginationControl::setDefaultViewPartial('../views/scripts/search/pagelist.phtml');
$paginator->setView($view);
}
}[/php]
SearchController.php这里面没什么可讲的了,大家着重看public function page($page,$numPerPage) 和public function pagelistAction() 两个函数就可以了因为实现分页主要增加了这两个函数。
好了 Zend_Paginator分页效果实例 代码全部完了
大家试下 如果有什么要修改的地方在下面跟帖
愿文地址:http://hi.baidu.com/haomao/blog/item/ef632a3401c9ef3d5bb5f5c6.html
[ 本帖最后由 sheak 于 2008-11-3 14:55 编辑 ] |