如何在codeIgniter中从控制器传递数组到模型

时间:2016-12-28 08:15:55

标签: php codeigniter model-view-controller model

我正在尝试将数组从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;
        }
    }
}
?>

1 个答案:

答案 0 :(得分: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;
        }
    }
}
?>