在cakephp中使用搜索结果分页
我已经实现了一个用户模型,我有一个简单的搜索表单 按性别、年龄、位置搜索。我正在用分页器 将结果分页。问题是它只是记住了 第一页上的搜索条件。当我点击第2页或下一页时 等它默认返回到搜索所有用户 这是我控制器中的脏搜索代码,基本上它只是检查 提交的表单字段,并对中的匹配字段进行查询 用户表,然后对结果进行分页:在cakephp中使用搜索结果分页,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我已经实现了一个用户模型,我有一个简单的搜索表单 按性别、年龄、位置搜索。我正在用分页器 将结果分页。问题是它只是记住了 第一页上的搜索条件。当我点击第2页或下一页时 等它默认返回到搜索所有用户 这是我控制器中的脏搜索代码,基本上它只是检查 提交的表单字段,并对中的匹配字段进行查询 用户表,然后对结果进行分页: if (!empty($this->data)) { // by name if (!empty($this->data['User']['search_
if (!empty($this->data)) {
// by name
if (!empty($this->data['User']['search_name'])) {
$this->paginate = array('conditions' => array('visible'=>1,
'OR'=>array(
'User.username LIKE' => '%'.$this->data['User']['search_name'].'%',
'User.firstname LIKE' => '%'.$this->data['User']['search_name'],
'User.lastname LIKE' => '%'.$this->data['User']['search_name'])
), 'limit'=>'10', 'order'=>'User.username');
}
// by gender
else if (!empty($this->data['User']['search_gender'])) {
$this->paginate = array('conditions' => array(
'visible'=>1,
'User.gender' => $this->data['User']['search_gender']
), 'limit'=>'10', 'order'=>'User.username');
}
// by state
else if (!empty($this->data['User']['search_state'])) {
$this->paginate = array('conditions' => array(
'visible'=>1,
'User.state' => $this->data['User']['search_state']
), 'limit'=>'10', 'order'=>'User.username');
}
// Send the results for the above criteria to the view
$results = $this->paginate('User');
$this->set('users', $results);
}
// Default retrieval of all users
else {
$this->paginate = array('conditions'=>array('visible'=>1),
'limit'=>'10', 'order'=>'User.username');
$this->set('users', $this->paginate('User'));
}
我正试图弄清楚如何制作
分页记住我的搜索条件。感谢您的帮助。
$this->data
在第2页是空的,因为它是通过发布您的搜索表单(而不是发布到第2页)填充的
将搜索表单post更改为GET(
$this->form->create('ModelName',array('type'=>'GET')
),并解析$this->params
,而不是$this->data
超级棒!对我也很有用。:/1我们也可以在会话的帮助下使用post方法来完成它,