【问题标题】:Codeigniter-passing value from controller to modelCodeigniter 将值从控制器传递到模型
【发布时间】:2017-11-24 12:02:23
【问题描述】:

我的英语不好,如有错误请见谅。我是 Codeigniter 的新手,我正在尝试使用此代码搜索记录,这是我的控制器代码:

public function search() {
    if(isset($_POST['search']))
    {
        $s = $_POST['search'];
        $this->db->select('u_id,name,email,phone');
        $this->db->from('user');
        $this->db->where('name =' ,"{$s}" );
        $query = $this->db->get();
        $res = $query->result();

        $data['user'] = null ;
        if($res)
        {
            $data['user'] = $res ;              
        }

        $this->load->view('filter' , $data);              
    }
}

它工作正常,但我想在单独的模型中编写这段代码。

$this->db->select('u_id,name,email,phone');
$this->db->from('user');
$this->db->where('name =' ,"{$s}" );
$query = $this->db->get();
$res = $query->result();

但我不知道如何将此变量值 $s = $_POST['search']; 传递给我的模型。有什么建议吗?

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    你可以试试下面的

    //in your application/models Folder put a File called UserSearch_Model.php
    
    class UserSearch_Model extends CI_Model
    {
        public function search($strSearch)
        {
            $query = $this->db
                ->select('u_id,name,email,phone')
                ->from('user')
                ->where('name' ,$s)
                ->get();
            return $query->result();
        }
    }
    
    
    //and in your controller
    
    public function search() 
    {
        $strSearch = $this->input->post('search');
        if (!empty($strSearch))
        {
            $data = [];
            $this->load->model("UserSearch_Model");
            $data['user'] = $this->UserSearch_Model->search($strSearch);
    
            if (is_array($data['user']) && count($data['user']) > 0)
            {
                $this->load->view('filter' , $data);
    
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试这个解决方案来解决您的问题:

      public function search() {
          $search_value = $this->input->post('search'));
          $this->db->select('u_id,name,email,phone');
          $this->db->from('user');
          if (isset($search_value) && !empty($search_value)) {
              $this->db->where('name',$search_value);
              // OR
              $this->db->like('name', $search_value);
          }
          $query = $this->db->get();
          $res = $query->result();
          $data['user'] = null;
          if ($res) {
              $data['user'] = $res;
          }
          $this->load->view('filter', $data);
      }
      

      我希望它会有所帮助。

      【讨论】:

      • 感谢您的建议,但我想为从 db 中选择记录创建模型,您刚刚修改了控制器的代码,这对我没有帮助,因为我的代码工作正常。
      【解决方案3】:

      控制器

      public function search() {
          $search = $this->input->post('search');
          $this->load->model('your_model'); //<-- You can load the model into "__construct"
      
          $search_result = $this->your_model->your_search_function($search); //<-- This is how you can pass a variable to the model from controller
      
          $this->load->view('your_view',['search_result'=>$search_result]); //<-- This is how you can load the data to the view. Hear your search result array is loaded on your view
      
      }
      

      型号

      public function your_search_function($search) {
      
      //You will receive this search variable ^^ here in your model
      
          $this->db->select('u_id,name,email,phone');
          $this->db->from('user');
          $this->db->where('name',$search);
          $query = $this->db->get();
          $result = $query->result_array();
      
          if(isset($result) && !empty($result))
          {
              return $result;
          } else 
          {
              return FALSE;
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-04
        • 2012-12-12
        • 1970-01-01
        相关资源
        最近更新 更多