網上不多有zend framework非fectall()分頁的中文參考,在此提供一個供新手參考php
//controler控制器方法 html
- //controler控制器方法
- public function findAction()
- {
- $this->view->results = "分頁測試 ";
- $basUser=new BasUser();
- //本身定義的model
- $count=$basUser->getAdapter()->fetchOne("SELECT count(*) FROM Bas_User");
- // 獲取basuser表的記錄總數,有些網上的案例使用fetchAll()方法,數據量大的時候不可行,目前用cout(*)優於fetchAll()
- //以後可考慮使用緩存
- $page = $this->_request->getParam('page', 1);
- //設定page,默認第一頁
- $rows=$basUser->fetchAll('true','userId',5,($page-1)*5); // 只查詢所須要的數據
- //從basuser表中查詢按userId排序
- $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($count));
- $paginator->setItemCountPerPage(5); // 每頁20條
- $paginator->setCurrentPageNumber($page);
- $this->view->basUsers=$rows;
- $this->view->paginator = $paginator ; //將paginator傳到view層
- $this->render("index/find",null,true);
- }
find.phtml view層緩存
- <html>
- <body>
- <style>
- /*分頁樣式*/
- .pages{font-size:12px; color:#000; text-align:center; zoom:1; padding:10px 0; }
- .pages a{display:inline-block;padding:3px 5px 2px 5px;text-indent:0;width:16px;}
- .pages span{font-size:12px; margin:0px 2px;padding:3px 5px 1px 5px;text-align:center;border:1px solid #D1E7FF;color:#075aaa;font-weight:bold;cursor:pointer;}
- .pages .pagesOn{color:#000; border-color:#fff}
- .pages .disabled{color:#999; border-color:#999; cursor:default;}
- </style>
- <h1>Example</h1>
- <table>
- <tr>
- <th>用戶名</th>
- <th>密 碼</th>
- </tr>
- <?php
- foreach ($this->basUsers as $key => $value) {
- ?>
- <tr>
- <td>
- <?php
- echo $value['userId'];
- echo $value['userName'];?>
- </td>
- <td>
- <?php
- echo $value['userPassword']?>
- </td>
- </tr>
- <?php
- }
- ?>
- </table>
- <?php echo $this->paginationControl($this->paginator,
- 'Sliding',
- 'index/pager.phtml'); ?>
- </body>
- </html>
pager.phtml分頁層ide
- <!--
- See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
- -->
- <?php if ($this->pageCount): ?>
- <div class="pages">
- <!-- Previous page link -->
- <?php if (isset($this->previous)): ?>
- <span><a href="<?php echo $this->url(array('page' => 1)); ?>">
- <<
- </a></span>
- <span><a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
- <
- </a></span>
- <?php else: ?>
- <span class="disabled"><<</span>
- <span class="disabled"><</span>
- <?php endif; ?>
- <!-- Numbered page links -->
- <?php foreach ($this->pagesInRange as $page): ?>
- <?php if ($page != $this->current): ?>
- <span><a href="<?php echo $this->url(array('page' => $page)); ?>">
- <?php echo $page; ?>
- </a></span>
- <?php else: ?>
- <span class="pagesOn">
- <?php echo $page; ?>
- </span>
- <?php endif; ?>
- <?php endforeach; ?>
- <!-- Next page link -->
- <?php if (isset($this->next)): ?>
- <span><a href="<?php echo $this->url(array('page' => $this->next)); ?>">
- >
- </a></span>
- <span><a href="<?php echo $this->url(array('page' => $this->pageCount)); ?>">
- >>
- </a></span>
- <?php else: ?>
- <span class="disabled">></span>
- <span class="disabled">>></span>
- <?php endif; ?>
- </div>
- <?php endif; ?>
注:網上流傳許多使用fetchAll()方法來查詢全部的記錄,而後使用$paginator = Zend_Paginator::factory($userArray);方法來定義$paginator,不可在數據量大的環境中使用,也就只能學習時使用!本文使用了學習
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($count)); 測試
方式來定義$paginator效率高於factory方式!fetch
若是提示找不到Zend_Paginator_Adapter_Null可手工加載一下:Zend_Loader::loadClass('Zend_Paginator_Adapter_Null'); this