Question

i have the following search form

<?php
echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));?>
<?php echo $this->Form->input('SearchTerm',array('label' => 'Find:')); ?>
<?php $options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'); ?>
<?php echo $this->Form->input('options', array('type'=>'select', 'label'=>'Search:', 'options'=>$options)); ?>

<?php echo $this->Form->end('search'); ?>  

<?php if($rs!=0){?>
<?php if($rs!=null) { ?>
    results found : <?php print $results; //print_r ($result['Order']['full_name']); ?>
<h1 class="ico_mug">Results Matching Term: <?php print '"'.$term.'"' ;?></h1>
<table id="table">
    <tr>

        <th>Client Name</th>
        <th>Order Code</th>
        <th>Phone</th>
        <th>Received Order</th>
        <th>Working On Order </th>
        <th>Order Ready </th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->

    <?php foreach ($rs as $order): ?>
    <tr>

        <td>
            <?php echo $this->Html->link($order['Order']['full_name'],
            array('controller' => 'orders', 'action' => 'view', $order['Order']['id'])); ?>
        </td>
        <td><?php echo $order['Order']['code']; ?></td>
        <td><?php echo $order['Order']['phone']; ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['receive_state']); ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['working_state']); ?></td>
        <td><?php echo $this->OrderStatus->getStatusString($order['Order']['ready_state']); ?></td>
        <td> <?php //echo $this->Html->link($this->Html->image("cancel.jpg"), array('action' => 'index'), array('escape' => false));?><?php echo $this->Html->link($this->Html->image("edit.jpg"), array('action' => 'edit',$order['Order']['id']), array('escape' => false));/*echo $this->Html->link('Edit', array('action' => 'edit', $order['Order']['id']));*/?></td>
    </tr>
    <?php endforeach; ?>
    <tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
    <?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';

    ?>

    <tr ><?php  //echo" << ".$this->Paginator->counter(
       // 'Page {:page} of {:pages}');
        ?></tr>

</table>

 <?php }else echo"no results found"; ?>


<?php } //endif?>

and this is my controller action

function search($options=null){
        $this->set('results',"");
        $this->set('term',"");
        $this->set('rs',0);    

        if ((isset($_GET["SearchTerm"]))||(isset($_GET["options"])) ) {
            $SearchTerm=$_GET["SearchTerm"];
            $options=$_GET["options"];

            if (!$options || !$SearchTerm) {
                $this->Session->setFlash('Please enter something to search for');
            }
            else {
                $SearchArray = array($options." LIKE " => "%".$SearchTerm."%");

                $this->paginate = array('conditions' =>  $SearchArray,'limit'=>2,'convertKeys' => array($options, $SearchTerm));
                $data=$this->paginate('Order');
                $this->set('rs', $data);
                $this->set('term',$SearchTerm);

            }
        }

as you can see i am using Get parameters options and SearchTerm. I want theese two to stay in the pagination links. I have tried various fixes that i've found on stackoverflow and on other site, (like ex:

$urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));

yet i still get the following error message :

Indirect modification of overloaded property PaginatorHelper::$settings has no effect [APP\View\orders\search.ctp, line 75

why is that? and what about a solution to this ? ( even if i use the wxample from the cookbook

$this->Paginator->settings['paramType'] = 'querystring';

i still get the same error :S can you please help/explain ?

Was it helpful?

Solution

If I understand your code correctly, you should be able to replace:

<?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';
?>

with

<?php $this->Paginator->options(array('url' => $this->passedArgs)); ?>

Form Fields In Pagination If you are actually trying to put the form elements into the pagination, you will need to alter your code slightly. You will need to build named parameters for each item in the search options. Then, in the action of the controller, you will need to check for both the request->data and the params['named'] versions of the search to make sure you use them in both cases. I would also clean up the code so it is a little more readable.

First, you need to use the cake methods for getting the data. It will make sure that it cleans the requests for you etc. In addition, you will need to account for the search term and filter options being passed as both a named variable and submitted in a form. So you need to update your controller as follows:

function search($options=null){

    $this->set('results',""); // where is this used ??
    $this->set('rs', 0); 
    $this->set('SearchTerm', '');
    $this->set('FilterBy', '');   
    $this->set('options', array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail'));

    if ($SearchTerm = $this->request->data['Order']['SearchTerm']) or $SearchTerm = $this->params['named']['SearchTerm']) {
        if ($FilterBy = $this->request->data['Order']['FilterBy'] or $FilterBy = $this->params['named']['FilterBy'])) {
            $this->paginate = array(
                'conditions' => array($FilterBy." LIKE " => "%".$SearchTerm."%"), 
                'limit' => 2, 
                'convertKeys' => array($FilterBy, $SearchTerm)
            );
            $this->set('rs', $this->paginate('Order'));
            $this->set('SearchTerm', $SearchTerm);
            $this->set('FilterBy', $FilterBy);
        } else {
            $this->Session->setFlash('Please enter something to search for');
        }
    } else {
        $this->Session->setFlash('Please enter something to search for');
    }
}

Next, we focus on the view. Remove move this:

$options=array('full_name'=>'By Name','code'=>'By Code','phone'=>'By Phone','email'=>'By Mail');

It shouldn't be in the view. It belongs in the controller (which is where it is now).

Next, cleanup the form:

<?php
  echo $this->Form->create('Order', array('action' => 'search','type'=>'get'));
  echo $this->Form->input('SearchTerm',array('label' => 'Find:'));
  echo $this->Form->input('FilterBy', array('type'=>'select', 'label'=>'Search:', 'options'=>$options));
  echo $this->Form->end('search'); 
?>  

*Note that I changed the options name to FilterBy so it is easier to find in the controller. Also there are other things that could be cleaned up in the view. However, I will only address the things that correspond to the question.

Now you need to replace this code:

<tr ><?php //echo $this->Paginator->numbers(array('first' => 'First page')); ?></tr>
    <?php
    $urlParams = $this->params['url'];
    unset($urlParams['url']);
    $optionss=array('url' => array('?' => http_build_query($urlParams)));
    //$this->Paginator->options($optionss);
    $this->Paginator->settings['paramType'] = 'querystring';

    ?>

    <tr ><?php  //echo" << ".$this->Paginator->counter(
       // 'Page {:page} of {:pages}');
        ?></tr>

</table>

With this code:

</table>
<p>
<?php
  $this->Paginator->options(array('url' => array_merge(array('SearchString' => $SearchString, 'FilterBy' => $FilterBy), $this->passedArgs)));

echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%')
));
?>  
</p>

<div class="paging">
    <?php echo $this->Paginator->prev('<< ' . __('previous'), array(), null, array('class'=>'disabled'));?>
 |  <?php echo $this->Paginator->numbers();?>
 |  <?php echo $this->Paginator->next(__('next') . ' >>', array(), null, array('class' => 'disabled'));?>
</div>

You an format it differently of course to fit your needs. But the code should work as expected with the search term and filter option applied to pagination.

Good luck and Happy Coding!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top