我希望有一个带有一个文本框、2 个下拉菜单和一个单选按钮(在 cakephp 中)的搜索表单。但这要通过分页来完成。搜索和分页可以单独工作,但不能一起工作。当我选择过滤条件并单击搜索按钮时,结果会在第一页上很好地显示,但如果我单击任何分页链接,过滤条件将丢失,并且分页数据显示时没有任何过滤器。我该如何解决这个问题?控制器代码:
private function _index() {
$this->genre_list();
$this->language_list();
$conditions = array();
debug($this->postConditions($this->data)); die;
if (!empty($this->request->data['Artist'])) {
foreach ($this->request->data['Artist'] as $name => $record) {
if (isset($record) && !empty($record)) {
if ($name == 'name') {
$conditions = array(
$this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
);
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
};
$this->paginate = array(
'contain' => array (
'User' => array(
'City',// => array('name'),
'State',// => array('name'),
'Country',// => array('name'),
),
'Genre',// => array('name'),
'Language',// => array('language'),
),
'limit' => 3,
'conditions' => $conditions
);
$data = $this->paginate($this->modelClass);
//~ debug($data);
$this->set(compact('data'));
}
看法:
<?php
echo $this->Form->create('Search', array(
'type' => 'file',
'inputDefaults' => array(
'format' => array(
'label', 'between', 'input', 'error', 'after'
)
),
'class' => 'form-horizontal'
));
echo $this->Form->input('Artist.name', array(
'div' => 'control-group',
'label' => array(
'class' => 'control-label',
'text' => 'Artist Name'
),
'between' => '<div class="controls">',
'after' => '</div>',
'placeholder' => 'Name',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
echo $this->Form->input('Artist.genre_id', array(
'div' => 'control-group',
'empty' => 'All',
'label' => array(
'class' => 'control-label',
'text' => 'Genre'
),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
echo $this->Form->input('Artist.language_id', array(
'div' => 'control-group',
'empty' => 'All',
'label' => array(
'class' => 'control-label',
'text' => 'Select Lanuage'
),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'style' => 'color:#B94A48'
)
)
));
?>
<?php echo $this->element('pagination');
使用会话编辑的代码
private function _index() {
$this->genre_list();
$this->language_list();
$conditions = array();
//~ foreach($this->request->params['named'] as $key => $record) {
//~ debug($this->request->params['named']);
//~ $this->request->data['Search'][$key] = $record;
//~ }
if (!empty($this->request->params['named']['page'])) {
// use session data for conditions
$conditions = (array)$this->Session->read('_indexConditions');
} else {
// delete session data
$this->Session->delete('_indexConditions');
}
$conditions = array();
if (!empty($this->request->data)) {
// new search! use the data to make conditions,
// like you did, and save the conditions
//~ if (!empty($this->request->data['Artist'])) {
foreach ($this->request->data['Search'] as $name => $record) {
if (isset($record) && !empty($record)) {
if ($name == 'name') {
$conditions = array(
$this->modelClass->User. 'User.name LIKE' => '%' . $record . '%',
);
} else {
$conditions[$this->modelClass . '.' . $name] = $record;
}
}
}
//~ }
$this->Session->write('_indexConditions', $conditions);
}
$this->paginate = array(
'contain' => array (
'User' => array(
'City',
'State',
'Country',
),
'Genre',
'Language',
),
'limit' => 3,
'conditions' => $conditions
);
$data = $this->paginate('Artist');
$this->set(compact('data'));
}