简体   繁体   English

Cake PHP分页问题

[英]Cake PHP pagination problem

I have a controller and view. 我有一个控制器并查看。 In the view it will displays all the records from the database. 在视图中,它将显示数据库中的所有记录。 Also there is a search by form in the top of that view page. 在该视图页面的顶部也有一个按表格搜索。 If we specify a search term and submit the form, the view will only displays the records resulting from that search. 如果我们指定搜索词并提交表单,则视图将仅显示该搜索结果。 The results are paginated. 结果是分页的。 But the problem is when I perform a search and click on the next page of the results, it will shows all the results. 但是问题是当我执行搜索并单击结果的下一页时,它将显示所有结果。

Here is my code. 这是我的代码。

View page index.ctp 查看页面index.ctp

echo $form->create('Apartment', array('action'=>'index'));
echo form->input('searchBy',array('type'=>'select','options'=>array('Id'=>'Id','User'=>'User','time'=>'Updated time')));
echo $form->input('query', array('type'=>'text', 'label'=>'Search Term'));
echo $form->end(array('name'=>'submit', 'label'=>'Search'));


<?php echo $this->element('pagination'); ?>

<th class="actions"><?php __('');?></th>
<th><?php echo $paginator->sort('id');?></th>
<th><?php echo $paginator->sort('Headline');?></th>
<th><?php echo $paginator->sort('Campaign','Campaign.Name');?></th>
<th><?php echo $paginator->sort('User', 'User.name');?></th>
<th><?php echo $paginator->sort('modified');?></th>
<th><?php echo $paginator->sort('status');?></th>
<th class="actions"><?php __('Actions');?></th>

Controller index function 控制器索引功能

if (!empty($this->data)) {
// Search 
switch($this->data['Apartment']['searchBy'])
{
case 'Id':
        $apartments = $this->paginate(NULL, array('Apartment.id' => $this->data['Apartment']['query']));
        break;
case 'User':
    $apartments = $this->paginate(NULL, array("User.name Like '%".$this->data['Apartment']['query']."%'"));
    break
case 'time':
    $apartments = $this->paginate(NULL, array("Apartment.modified Like '%".$this->data['Apartment']['query']."%'"));
    break;
}
}
else {
        $apartments = $this->paginate();
}

Solved the issue. 解决了问题。 Got details from AdvancedPagination AdvancedPagination获得了详细信息

Your view sample looked incomplete, but I would first check to make sure that you're including the prev and next methods from the PaginationHelper in the view to make sure the search criteria is propagated. 您的视图示例看起来不完整,但是我首先要检查一下,以确保您在视图中包括PaginationHelper中的prevnext方法,以确保传播搜索条件。

<?php
    echo $paginator->prev('« Previous ', null, null, array('class' => 'disabled'));
    echo $paginator->next(' Next »', null, null, array('class' => 'disabled'));
?> 

Also, I hope you're sanitizing the search parameters you're passing to paginator somewhere before hand. 另外,我希望您能对传递给分页器的搜索参数进行消毒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM