本文介绍ZF2自带的分页
示例表(article)
ID(id) | 用户id(uid) | 标题(title) |
1 | 1 | Timor队长正在待命 |
2 | 2 | 土豆呼叫地瓜 |
3 | 3 | 地瓜收到请回答 |
3 | 1 | 德玛西亚 |
Step 1:添加数据操作表(ArticleTable)
(path: /Module/Application/src/Application/Model/ArticleTable.php)
<?php /** * 分页测试,数据查询表 * * @author Star <wmistar@gmail.com> * @license http://mushroot.com * @version: 1.0 * */ namespace Application\Model; use Zend\Db\TableGateway\TableGateway; class ArticleTable { private $tableGateway; public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; } /** * 文章分页信息 * * @return \Zend\Paginator\Paginator */ public function getAllArticle() { $select = $this->tableGateway->select(); $select->columns(array('id','uid','title')); $adapter = new \Zend\Paginator\Adapter\DbSelect($select, $this->sql); $paginator = new \Zend\Paginator\Paginator($adapter); return $paginator; } }
Step 2:在Module.php中注入当前文章数据库查询类
(path: /Module/Application/Module.php)
use Application\Model\ArticleTable; public function getServiceConfig() { return array( 'factories' => array( 'Table\Application\Article' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); return new ArticleTable( new TableGateway('ys_city', $dbAdapter) ); } ) ); }
Step 3:在Controller中调用分页信息
<?php /** * * @author Star * @copyright © 2013-2113 yousha.me * @license http://www.yousha.me * @version: file_name 1.0 2013-7-26 下午7:53:14 Star * */ namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; class IndexController extends AbstractActionController { private $articleTable; public function indexAction() { $articleTable = $this->getArticleTable(); $paginator = $$paginator->getAllArticle(); //设置当前页码 $paginator->setCurrentPageNumber($this->getRequest()->getQuery('page')); //设置每页数量(在这里设置2条数据) $paginator->setDefaultItemCountPerPage(2); return array( 'page' -> $paginator, ); } /** * 返回文章信息查询表表 * * @return \Application\Model\ArticleTable */ private function getArticleTable() { if (!$this->articleTable) { $sm = $this->getServiceLocator(); $this->userTable = $sm->get('Table\Application\Article'); } } }
Step 4:在视图中设置分页信息
建立视图文件page.phtml
(path: /module/Application/views/application/index/page.phtml)
<?php if ($this->pageCount): ?> <div> <!-- First page link --> <?php if (isset($this->previous)): ?> <a href="?page=<?php echo $this->first; ?>"> 首页 </a> | <?php else: ?> <span>首页</span> | <?php endif; ?> <!-- Previous page link --> <?php if (isset($this->previous)): ?> <a href="?page=<?php echo $this->previous; ?>"> < 上一页 </a> | <?php else: ?> <span>< 上一页</span> | <?php endif; ?> <!-- Numbered page links --> <?php foreach ($this->pagesInRange as $page): ?> <?php if ($page != $this->current): ?> <a href="?page=<?php echo $page; ?>"> <?php echo $page; ?> </a> | <?php else: ?> <a style="color: blue;"> <?php echo $page; ?> </a> | <?php endif; ?> <?php endforeach; ?> <!-- Next page link --> <?php if (isset($this->next)): ?> <a href="?page=<?php echo $this->next; ?>"> 下一页 > </a> <?php else: ?> <span>下一页 ></span> <?php endif; ?> <!-- Last page link --> <?php if (isset($this->next)): ?> <a href="?page=<?php echo $this->last; ?>"> 末页 </a> <?php else: ?> <span>末页</span> <?php endif; ?> </div> <?php endif; ?>
Step 5:在Index视图文件中调用page视图
(path: /module/Application/views/application/index/index.phtml)
<?php if (0 < count($this->page)) : ?> <table> <tr> <td>ID</td> <td>Title</td> </tr> <?php foreach ($this->page as $item) : ?> <tr> <td><?php echo $item->id;?></td> <td><?php echo $otem->title;?></td> </tr> <?php endforeach;?> </table> <?php endif; ?> <?php echo $this->paginationControl($this->page, 'Sliding', 'page/list',array()); ?>