【问题标题】:How to pass an array to the model from the controller in codeIgniter如何在codeIgniter中将数组从控制器传递给模型
【发布时间】:2016-12-28 08:15:55
【问题描述】:

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

【问题讨论】:

  • 嗨,你能把日志错误贴出来吗?

标签: php codeigniter model-view-controller model


【解决方案1】:

在您的模型中尝试以下代码::

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;
        }
    }
}
?>

【讨论】:

  • 消息:未定义属性:CI_Loader::$SearchModel
  • 还是一样的消息:未定义的属性:CI_Loader::$searchmodel
  • 你把模型放在哪里了?
  • 必须在应用程序/模型中,而不是在应用程序/库中。
  • 我解决了你的解决方案是正确的只是一个问题只需替换 $data = $this->load->searchmodel->getCadre($type,$value); with $data = $this->searchmodel->getCadre($type,$value);
猜你喜欢
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多