views:

1482

answers:

5

Problem: when i have a search resultset with pagination, the links next, prev and numbers do not keep the search parameters. Seems to be a common problem.

I searched everywhere on the internet, and at last i found that i should put this statement in the view:

$paginator->options(array('url' => $this->passedArgs));

However, i can't make it work, Should i do something on $this->passedArgs in the controller?

Please help

Thanks


controller code:

function search($category=null) 
{ 
        $this->paginate['Cat'] = array( 
        'limit' => 10, 
        'order' => array ('Cat.id' => 'desc') 
        ); 


      $conditions = array('Cat.category' => $this->data['Cat'] 
['category']); 
      $this->set( 'data', $this->paginate('Cat', $conditions ) ); 
      $this->render( 'index_ar' ); 


      return; 



}

view code:

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


<table class='grid'> 
<tr> 
<th><?php echo $paginator->sort('ID', 'id'); ?></th> 
<th><?php echo $paginator->sort('Nome', 'name'); ?></th> 
<th><?php echo $paginator->sort('Categoria', 'category'); ?></th> 
<th>Foto</th> 
<th><?php echo $paginator->sort('Stato', 'status'); ?></th> 
<th width='25%'></th> 
</tr> 
        <?php $i = '0'; $count = '1';?> 
        <?php foreach ($data as $cats):  ?> 
                <?php $class = (is_int($i/2)) ? 'data-grid-row-1' : 'data-grid- 
row-2';?> 
                <tr class="<?php echo $class?>"> 
                        <td><?php echo $cats['Cat']['id'] ?></td> 
                        <td><?php echo $cats['Cat']['name'] ?></td> 
                        <td><?php echo $cats['Cat']['category'] ?></td> 
                        <td style='width:25px'> 


[cut]
A: 

seems correct. make sure passedArgs is not empty. you can always pass a test array to url to make sure it's working.

$paginator->options(array('url' => array('a','b')));
Funky Dude
A: 

Thats my problem, passedArgs is null. How can i set it in the controller?

I should do, for example,

$this->passedArgs['url'] = array ( 'a', 'b' ); ???

Sunchaser
A: 

$this->passedArgs should be used if you are using passedArgs to contain the category and search term you are searching for i.e. url params in the form "/category:XXXX/term:YYYY" etc.

If you are just using normal params without the 'category:' or 'term:' prefixes, then you need to use $this->params['pass']

If you are not passing the category and term around in the url, then you should be.

Whenever I have a result set that users can filter by a search term or category or anything I always transfer the posted form data into url params and redirect the user to that url, then take the params from the url to populate the conditions for pagination.

This is a commonly used design pattern and considered best practice as it allows users to deep link to search results without having to fill in a form.

If you are implementing a site search functionality, I have a fairly complete cakephp search plugin available on my github, but no documentation just yet, but have a look in the searches controller for an idea what I mean.

neilcrookes
A: 

Hello I solved the problem by setting passedArgs in a correct way. Posting working code:

Controller:

function search()
{
    $category = 'All';

    //debug( $this->passedArgs );
    //debug( $this->params);
    //debug( "form categ: " . $this->data['Cat']['category']) ;


    $conditions = array();

    // if category is passed with a submit...
    if ( isset( $this->data['Cat']['category'] ) )
      $category = $this->data['Cat']['category'];
    // if category is passed through paginating urls...
    if ( isset( $this->passedArgs['category'] ) )
      $category = $this->passedArgs['category'];

    // build conditions

    if ( $category != 'All')
      $conditions = array ( 'Cat.category' => $category );

    //set passedArgs for building paginating url in a correct way    
    if ( !isset( $this->passedArgs['category'] ) ) 
      $this->passedArgs['category'] = $category ;  
    $this->set( 'data', $this->paginate('Cat', $conditions ) );    
    $this->render( 'index_ar' );    
    return;
}

View:

<center>
<h2>heading</h2>

<?php echo $form->create( 'Cat', array('action' => 'search')) ?>
<table border="0">
<td>Category</td>
<td valign="top">
<?php echo $form->input('category', 
array( 
'label' => false, 
'options' => 
array(
'all' => 'All',
'cat1' => 'Category1'   
)
) ); ?>
</td>
</tr>
-->
<tr><td colspan="2" valign="top"><?php echo $form->end('Search'); ?></td></tr>
</tr>
</table>
<hr />
<?php $paginator->options(array('url' => $this->passedArgs ));?>
<?php echo $paginator->counter( array('format' => 'Page %page% of %pages%, record  %current% on %count%'));?>
<br/>
Pages: <?php echo $paginator->numbers( ); ?>
<br/>
<?php
if ( count($data) == 0 )
    echo "<p style='text-align:center'>No record found.</p>";
else
{
?>
<table class='grid'>
<tr>
<th align="left"><?php echo $paginator->sort('ID', 'id'); ?></th>
<th align="left"><?php echo $paginator->sort('Name', 'name'); ?></th>
<th align="left"><?php echo $paginator->sort('Category', 'category'); ?></th>
<th align="left">Foto</th>
<th align="left"><?php echo $paginator->sort('Status', 'status'); ?></th>
<th width='17%'></th>
</tr>
[...]
Sunchaser
A: 

I just had the same problem, after a bit of playing...

<?php $paginator->options(array('url' => $this->params['named'])); ?>

worked for me. (I'm using CakePHP 1.2) Hope this helps...

Jenski