Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp中搜索过滤器的分页行为_Cakephp_Search_Pagination_Cakephp 2.1 - Fatal编程技术网

cakephp中搜索过滤器的分页行为

cakephp中搜索过滤器的分页行为,cakephp,search,pagination,cakephp-2.1,Cakephp,Search,Pagination,Cakephp 2.1,我希望有一个带有一个文本框、两个下拉列表和一个单选按钮(cakephp)的搜索表单。但这是通过分页来完成的。搜索和分页可以单独工作,但不能同时工作。当我选择筛选条件并单击“搜索”按钮时,结果在第1页上显示良好,但如果我单击任何分页链接,筛选条件将丢失,分页数据将在没有任何筛选的情况下显示。我如何解决这个问题? 控制器代码: private function _index() { $this->genre_list(); $this->language_list();

我希望有一个带有一个文本框、两个下拉列表和一个单选按钮(cakephp)的搜索表单。但这是通过分页来完成的。搜索和分页可以单独工作,但不能同时工作。当我选择筛选条件并单击“搜索”按钮时,结果在第1页上显示良好,但如果我单击任何分页链接,筛选条件将丢失,分页数据将在没有任何筛选的情况下显示。我如何解决这个问题? 控制器代码:

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'));
}
视图:


答案很简单:将搜索条件保存在会话或cookie中,如果没有发送新的搜索条件,则使用这些条件

为了简单起见,我省略了您的大部分代码。这样的办法应该行得通

private function _index() {
  // check if this is a pagination request without data
  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
    $this->Session->write('_indexConditions', $conditions);
  }

  $this->paginate = array(
    'conditions' => $conditions
  );
  $data = $this->paginate($this->modelClass);
  $this->set(compact('data'));
}

它应该被包装在一个组件中。在我的一个项目中,我有一个组件可以自动执行此操作,并替换控制器数据以实现更自动化的过程。

在不使用会话的情况下获得了解决方案

在控制器中:

if (!empty($this->request->data) || $this->request->params['named']) {
        foreach($this->request->params['named'] as $key => $record) {
            if($key!= 'page')
                $this->request->data['Search'][$key] = $record;
        }
        foreach ($this->request->data['Search'] as $name => $record) {
            if (isset($record) && !empty($record)) {
                $this->request->params['named'][$name] = $record;
                if ($name == 'name') {
                    $conditions[$this->{$this->modelClass}->User. 'User.name LIKE'] = '%' . $record . '%';
                } else {
                    $conditions[$this->modelClass . '.' . $name] = $record;
                }
            }
        }
    }
并在视图中明确提供控制器和操作:

echo $this->Form->create('Search', array(
    'type' => 'file',
    'inputDefaults' => array(
        'format' => array(
            'label', 'between', 'input', 'error', 'after'
        )
    ),
    'url' => array(
        'controller' => 'artists',
        'action' => 'index',
    ),
    'class' => 'form-horizontal'
));

解决了这个问题。

在2.x中,您可以将表单设置为使用GET而不是POST

将表单的类型从file改为GET(顺便说一句,为什么使用file??)

当然,也不用使用请求中的数据数组

$this->request->data['Search']['name'] 
您将使用如下查询数组:

if (!empty($this->request->query['name'])) {

嘿,我试过了,但仍然不起作用,我已经编辑了我的帖子,包括修改过的代码。我哪里做错了?
echo $this->Form->create('Search', array(
'type' => 'get',
$this->request->data['Search']['name'] 
if (!empty($this->request->query['name'])) {