2016-12-28 94 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

放置模型的位置? –