Zend Framework Pagination

Zend Framework Pagination

Hold on Cowboy

This blog post is pretty old. Be careful with the information you find in here. It's likely dead, dying, or wildly inaccurate.

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.phppublic 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

/views/scripts/index/list.phtml

paginator): ?>
paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>
paginator as $item): ?>
paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>

Credit: http://www.zfforums.com/zend-framework-components-13/databases-20/problem-zend_paginator-dynamic-select-query-2209.html#post7087