2016-12-28 92 views
0

我想从codeIgnitor中的控制器传递数组到模型中我知道这个问题有一些答案,但我尝试了所有这些,没有解决方案与我合作。所以这个我的代码:如何将数组传递到codeIgnitor中的控制器模型

控制器:

<?php 
if(! defined('BASEPATH')) exit('No direct script access allowed'); 
class SearchController extends CI_Controller{ 
    var $controller = "user"; 
    public function index(){ 
     $this->home(); 
    } 
    public function home(){ 
     $this->load->view("search_view"); 
    } 
    public function search(){ 
     if(isset($_POST['submit'])){ 
      $data['type']=$this->input->post('type'); 
      $data['value']=$this->input->post('value'); 
      $this->load->model("SearchModel"); 
      $this->load->SearchModel->getCadre($data); 
     }       
    } 
} 
?> 

型号:

<?php 
class SearchModel extends CI_Model{ 
    function construct(){ 
     parent::__construct(); 
     $this->getCadre(); 
    } 
    function getCadre(){ 
     $query = $this->db->get('cadres'); 
     $query = $this->db->where($type,$value);//the argument witch i want to take from array 
     if($query->num_rows()>0){ 
      $values = result(); 
      return $query->result(); 
     } 
     else{ 
      return NULL; 
     } 
    } 
} 
?> 
+0

嗨,你能发布日志错误吗? – zatamine

回答

2

尝试下面的代码在你的模型::

class SearchModel extends CI_Model{ 
    function construct(){ 
     parent::__construct(); 
    } 
    function getCadre($data = array()){ 

     $this->db->where($data);//the argument witch i want to take from array 
     $query = $this->db->get('cadres'); 
     if($query->num_rows()>0){ 
      // $values = result(); 
      return $query->result(); 
     } 
     else{ 
      return NULL; 
     } 
    } 
} 

或者你可以如下传递个人数据并返回结果...同时尝试如下

控制器:

if(! defined('BASEPATH')) exit('No direct script access allowed'); 
class SearchController extends CI_Controller{ 
    var $controller = "user"; 
    public function index(){ 
     $this->home(); 
    } 
    public function home(){ 
     $this->load->view("search_view"); 
    } 
    public function search(){ 
     if(isset($_POST['submit'])){ 
      $type=$this->input->post('type'); 
      $value=$this->input->post('value'); 
      $this->load->model("searchmodel"); 
      $data = $this->searchmodel->getCadre($type,$value); 
      if($data==NULL){ 
       echo 'No records found!!'; 
      } 
      else 
       { 
       //send data to view 
       $this->load->view('view_name',$data); 
       } 
     }       
    } 
} 

型号

class SearchModel extends CI_Model{ 
    function construct(){ 
     parent::__construct(); 
     $this->load->database(); 
    } 
    function getCadre($type,$value){ 

     $this->db->where('type',$type);//the argument witch i want to take from array 
     $this->db->where('value',$value); 
     $query = $this->db->get('cadres'); 
     if($query->num_rows()>0){ 
      // $values = result(); 
      return $query->result(); 
     } 
     else{ 
      return NULL; 
     } 
    } 
} 
?> 
+0

消息:未定义的属性:CI_Loader :: $ SearchModel – Amado

+0

仍然相同消息:未定义的属性:CI_Loader :: $ searchmodel – Amado

+0

放置模型的位置? –