使用cakePHP进行搜索

使用cakePHP进行搜索,php,cakephp,view,controller,Php,Cakephp,View,Controller,首先,我有一个连接到users表的模型user.php。 我有一个控制器用户控制器 我创建了一个搜索视图:(文件名:index.ctp) 这将加载search.ctp 我的问题是,当我在search.ctp中使用变量$users时,它会给我一个未定义的错误变量:users[APP\views\users\search.ctp,第10行] 我不明白。 请帮忙。谢谢 您正在为输入指定一个自定义名称,然后检查$this->data,该名称将为空,因为您输入的名称不正确(并且不会自动填充到$this->

首先,我有一个连接到users表的模型user.php。 我有一个控制器用户控制器

我创建了一个搜索视图:(文件名:index.ctp)

这将加载search.ctp

我的问题是,当我在search.ctp中使用变量$users时,它会给我一个未定义的错误变量:users[APP\views\users\search.ctp,第10行]

我不明白。
请帮忙。谢谢

您正在为输入指定一个自定义名称,然后检查
$this->data
,该名称将为空,因为您输入的名称不正确(并且不会自动填充到
$this->data
)。使用以下命令

echo$this->Form->input(“txt_搜索”,数组('label'=>'search label'))

有几件事你应该看看

  • 用户
    变量设置一个默认值,这样,如果他们直接请求,页面不会中断。Have
    $result=array()并在search.ctp中检查它

  • 为什么要在输入中指定操作属性?你不需要那个


  • 将调试级别至少提高到2以查看错误和SQL转储,同时在控制器中放置
    debug($results)
    以查看其内容,然后再将其传递给视图。是否确定if代码正在运行且查询正在返回结果?
    <p><?php
    echo $this->Form->create("Users", array('action' => 'search'));
    echo $this->Form->input("Search Label", array('action' => 'search', 'name' => 'txt_search'));
    echo $this->Form->end("Search");
    ?></p>
    
    function search() {
      if (!empty($this->data))
      {
        $name = $this->data['Users']['txt_search'];
        $conditions = array("User.name Like " => "%$name%");
        $result = $this->User->find('all', array('conditions'=> $conditions));
        $this->set('users', $result);
      }
    }