views:

413

answers:

2

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

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();
}
A: 

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.

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

andymism
i have already had added the pagination element in it.<?php echo $this->element('pagination'); ?>
Jasim
+1  A: 

Solved the issue. Got details from AdvancedPagination

Jasim