2

我想使用Zend_Paginator对搜索结果进行分页。所以我将我的数据传递给一个分页器实例:

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

数据以这种方式返回

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由我的搜索实现提供。这一切都很好,我得到了正确的数据和分页链接显示。

但是我无法对结果进行分页,因为分页链接无效。所以正常的分页会有以下链接:

mysite.de/home/index/page/1

在搜索中我现在有

mysite.de/home/search/page/1

这不起作用。任何建议如何实现搜索分页?

编辑:我有HomeController两个动作,索引和搜索动作。IndexAction 显示所有数据,我可以对其进行分页。

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;
}

searchActions 处理搜索过程:

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;
}

因此,就像您在搜索操作中看到的那样,当我分页时,搜索过程存在问题。我需要以某种方式决定是搜索还是分页。对此有何建议?

4

4 回答 4

2

由于搜索需要搜索参数分页将失败,因为分页时搜索参数为空。

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

因此,每当我们对搜索进行分页时,我们都需要传递此参数。我使用以下方法解决了这个问题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;
}

我在我的 searchAction 顶部有这个,一切正常。

于 2011-03-31T19:50:27.797 回答
1
public function search()

你确定这里没有打错?应该

public function searchAction()
于 2011-03-30T20:07:43.873 回答
1

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);
于 2011-03-30T19:26:50.583 回答
0

您将搜索数据放入但使用(为空)$response创建分页器实例$data

于 2011-03-31T12:54:45.747 回答