DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

How to paginate search results?

I want to paginate search results using Zend_Paginator. So I pass my data to a paginator instance:

$paginator = new Zend_Paginator ( 
           new Zend_Paginator_Adapter_DbSelect ( $data ) 
);

Data is returned this way

public function getData($idArray){
        $db = Zend_Db_Table::getDefaultAdapter();
        $selectProgramme = new Zend_Db_Select($db);

        $selectProgramme->from('programme')
                            ->order('id DESC')
                            ->where('id IN(?)', $idArray);

        return $selectProgramme;        
}

$idArray is provided by my search implementations. This all works great and I get the correct data and pagination links displayed.

However I can't paginate the result because the pagination links are not valid. So normal pagination would have following link:

mysite.de/home/index/page/1

in search I now have

mysite.de/home/search/page/1

This does not work. Any suggestions how to implement search pagination?

EDIT: I have a HomeController with two actions, index and search action. IndexAction displays all data and I can paginate it.

public function indexAction(){
    //...
    $paginator = new Zend_Paginator(
                 new Zend_Paginator_Adapter_DbSelect($data)
    );
    $paginator->setItemCountPerPage(16)
              ->setPageRange(20)
              ->setCurrentPageNumber($this->_getParam('page', 1));

    $this->view->data = $paginator;
}

The searchActions handles the search process:

public function searchAction(){
    $response = $solr->search($this->getRequest()->getParam('search', null));
    //...if items found get the data exactly the same way as in the 
    // index action, using Zend_Paginator_Adapter_DbSelect
    $paginator = new Zend_Paginator(
                 new Zend_Paginator_Adapter_DbSelect($data)
    );
    $paginator->setItemCountPerPage(16)
              ->setPageRange(20)
              ->setCurrentPageNumber($this->_getParam('page', 1));

    $this->view->data = $paginator;
}

So like you see in the search action there is a problem with the search process when I paginate. I need to decide somehow if to search or to paginate. Any suggestions on that?

Upvotes: 2

Views: 2254

Answers (4)

DarkLeafyGreen
DarkLeafyGreen

Reputation: 70416

Since search required the search parameter pagination will fail because when paginating the the search parameter is null.

$sreq = $this->getRequest()->getParam('search', null);

So we need to pass this parameter whenever we paginate our search. I solve this using Zend_Session:

//get search param
$sreq = $this->getRequest()->getParam('search', null);
//store search param in session for pagination
$search = new Zend_Session_Namespace('PSearch');

if($sreq != null){
    $search->psearch = $sreq;
}else{
   $sreq = $search->psearch;
}

I have this at the top of my searchAction and everything works.

Upvotes: 2

Tomáš Fejfar
Tomáš Fejfar

Reputation: 11217

You put your search data into $response but create paginator instance using $data (which is null)

Upvotes: 0

Ololo
Ololo

Reputation: 1097

public function search()

Are you sure that you didn't mistyped here? Should be

public function searchAction()

Upvotes: 1

robertlbolton
robertlbolton

Reputation: 168

Not sure I understand, but is your problem that the page parameter from the url is not making it's way to the Paginator - e.g. regardless of what page you are on, it is always showing the first 20 results?

If so, have you tried manually setting the page on the paginator:

$page = $this->_getParam('page', 1);
$paginator->setCurrentPageNumber($page);

Upvotes: 1

Related Questions