English 中文(简体)
Cakephp 1.2 Paginator and PassedArgs
原标题:

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] 
问题回答

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

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

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>
[...]

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





相关问题
Pagination problem

I m using this code for my pagination, and I d like the user s choice to be persistent throughout the site (this has been solved so far)...the only problem now is that the session variable now is ...

mod rewrite problem with 2 parameters

I m trying to rewrite the categoy file of my shop system, I also integrated a pagination so I need to rewrite 2 parameters. it almost works, otherwise I wouldn t be posting in here this is the ...

Issues with pagination in ASP.NET MVC

I am trying to implementation the same pagination that is used in the NerdDinner ASP.NET. I am receiving the following error in my view, whenever the pagination starts to kick in. "A route named ...

Pagination in SQL Server

How do i limit the result of a query (in my case about 60K rows) and select only from the X row to the Y row? If I use ROW_NUMBER() I don t like my query because it involves 2 select queries .. one ...

Cakephp 1.2 Paginator and PassedArgs

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

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

热门标签