0

我正在尝试在 CakePHP 2.x 中添加分页,并在表单中进行条件搜索。但是,在视图中它没有给出我的情况的下一页。这是我的代码:

控制器

public function result($palabraclave = null)
    {
      $this->layout = 'tempart';
          $this->paginate = array(
            'limit' => 5,
            'order' => array(
                'Articulo.created' => 'desc'),
                'conditions'=>array("titulo like ?"=>"%".$this->data['Paginas']['palabraclave']."%") 
        )
       $data = $this->paginate('Articulo');//Consulta a la base de datos
        $this->set(compact('data'));
    }

如果我单击“下一步”查看搜索的下一页结果,它不会将分页条件从第 1 页传递到第 2 页。:(

看法:

    <div style="display: inline-block;  width:70%;" id="test">
<?php foreach ($data as $da): ?>

<br />

<article>   
<?php echo $this->Html->image('article_preview/mini_'.$da['Articulo']['imagen'], array('alt' => 'Hospital-Excel'))?>
<div style="text-align:left;">
<?php echo $this->Html->link("<h6 class='artnombre'>".$da['Articulo']['titulo']."<h6>", array('class'=>"artnombre",'action'=> 'articulo',$da['Articulo']['id']),array('escape' => false)); ?> 

</div>
<br />
<p> 
<?php echo $da['Articulo']['descripcion']; ?>               <?php echo $this->Html->link('Read more ....', array('action'=> 'articulo',$da['Articulo']['id'])); ?>  
<h5></h5>
</p>
<div class="puntoshorizontal" style="clear"></div>
</article>  
<?php endforeach; ?>



<div style="text-align:center">
<?php
// Shows the page numbers
// Shows the next and previous links
echo $this->Paginator->prev('« Back', null, null, array('class' => 'disabled')) . " |";
echo $this->Paginator->numbers(). "| "; 
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo "<br />";
echo $this->Paginator->counter();
// prints X of Y, where X is current page and Y is number of pages


?>
</div>

</div>
4

3 回答 3

1

一种在每个页面上保留搜索参数的方法,您应该执行以下操作:发布数据后,在您的方法中,您有 $this->data['Paginas']['palabraClave'] 然后将其保存到会话中。因此,在随后对 /resultados/pag1,2,..n 的每次调用中,您都会从会话中获得 palabra clave 的值。

最后你会得到类似的东西:

public function resultados(){
    $conditions = array();

    if($this->Session->check('conditions')){
      $conditions = $this->Session->read('conditions');
    }

    if(isset($this->data)){
        $conditions = array("titulo LIKE" => "%".this->data['Paginas']'palabraclave']"%",));
    //save to the session
    $this->Session->write('conditions',$conditions);
    }
    $data = $this->painate('Articulo',$conditions);
    $this->set('data');
}

这可能需要一些调整,但我认为这个想法很明确。

于 2013-06-06T15:51:45.133 回答
0

在调用 paginate 方法时设置分页条件,如下所示:

$data = $this->paginate('Articulo', array(
    "titulo LIKE" => "%" . $this->data['Paginas']['palabraclave'] . "%",
));

而不是在分页属性中。

于 2012-10-04T00:03:53.553 回答
0

这是示例:

1:将所有 URL 参数传递给分页器函数

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

2:常见错误:不保持条件搜索与分页

if($this->data){
    $this->passedArgs = array(
       'name' => $this->data[‘User’]['name']
    );
}else{
    if(isset($this->passedArgs['name'])){
         $this->data[‘User’]['name'] = $this->passedArgs['name'];
    }else{
         $this->data[‘User’]['name'] = ;
    }
}
于 2014-07-01T09:16:32.797 回答