- July 2010 (1)
- June 2010 (4)
- May 2010 (3)
- April 2010 (6)
- March 2010 (6)
- February 2010 (8)
- January 2010 (13)
- December 2009 (4)
- October 2009 (2)
- September 2009 (1)
Zend Framework Pagination
This is a way to have Zend Framework Pagination (Paginator) working so that it remembers the query across mulitiple pages. This seemed like such a simple and obvious task, but the solution is not so obvious. The trick is to test if 'isPost()' then save the query to the session variable. That way when a visitor travels to page two we have the query in the session variable. No URL tricks or other tom foolery.
/IndexController.php
public function searchAction() { $value = $this->_request->getPost('query'); // Start a session $session = new Zend_Session_Namespace('value'); // If $value is a post then the search has just been submitted. if ($this->getRequest()->isPost()) { $session->value = $value; } // Get the select from Zend_DB $amazon = new Model_Example(); $result = $amazon->searchExample($session->value); // Assign Paginator data to view $this->view->paginator = $this->_addPaginator($result); $this->view->query = $session->value; $this->_helper->viewRenderer('list'); } private function _addPaginator($select) { $page = $this->_getParam('page', 1); $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select)); $paginator->setItemCountPerPage(self::ItemCountPerPage) ->setCurrentPageNumber($page) ->setPageRange(5); return $paginator; }
/layout/scripts/layout.phtml
<div id="search"><form action="/index/search" method="POST"><input type="text" name="query" id="query" value="<?php echo $this->query; ?>"><input type="submit" name="Submit" value="Submit"></form></div>
/views/scripts/index/list.phtml
<?php if($this->paginator): ?> <div><?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?></div> <?php foreach($this->paginator as $item): ?> <!-- list your item data --> <?php endforeach; ?> <div><?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?></div> <?php endif; ?>
Credit:
http://www.zfforums.com/zend-framework-components-13/databases-20/proble...
Comments
Post new comment