将数组作为参数传递给PHP CodeIgniter中的模型

时间:2018-02-27 04:27:05

标签: php codeigniter parameters codeigniter-3

我试图在我的Array as parameter申请中将Stored Procedure传递给Code-Igniter。当我打印该数组时,它会为我提供所选值的正确结果。但它在我的模型函数中给出了一个错误。

这是我的控制器:

public function index()
{
        $propertyType=$this->input->post('property_type');
        $area=$this->input->post('area_id');
        $clustersID['cluster']=$this->input->post('cluster_id');
        $stageId=$this->input->post('property_status');
        print_r($clustersID);
        $data['properties'] = $this->p->getPropertyByAreaCluster($propertyType, $area, $clustersID, $stageId);

            // echo "<pre>";
            // print_r($data['properties']);
            // echo "</pre>";
            // exit();
            //$data['props'] = $this->p->PropView($propId);
            $this->load->view('template/header', $data);
            $this->load->view('Property/property_view', $data);
            $this->load->view('template/footer');
    }

这是我的模特:

public function getPropertyByAreaCluster($propertyType, $area, $clustersID, $stageId)
    {
        $query = $this->db->query("call fetch_propertyType_Area_Cluster_Stage($propertyType,$area,$clustersID,$stageId)");
        if ($query) {
            $data = $query->result();
            $query->next_result(); 
            $query->free_result();
            return $data;
        }else{
            return false;
        }
    }

错误:

  

遇到严重错误:通知

     

消息:数组到字符串转换

     

文件名:models / Property_m.php

     

行号:26

     

回溯:

     

文件:/opt/lampp/htdocs/livemg/application/models/Property_m.php行:   26功能:_error_handler

     

文件:/opt/lampp/htdocs/livemg/application/controllers/Property.php   行:61功能:getPropertyByAreaCluster

     

文件:/opt/lampp/htdocs/livemg/index.php行:315功能:   require_once

     

发生数据库错误错误号:1054

     

未知列&#39;数组&#39;在&#39;字段列表&#39;

     

调用fetch_propertyType_Area_Cluster_Stage(0,0,Array,0)

     

文件名:models / Property_m.php

     

行号:26

2 个答案:

答案 0 :(得分:0)

我得到了一个答案,只需将该数组转换为字符串并传递它。

    public function index()
    {
        $propertyType=$this->input->post('property_type');
        $area=$this->input->post('area_id');
        $clustersID['cluster']=$this->input->post('cluster_id');
        $clusterString = implode(',', $clustersID['cluster']);

        echo "<pre>";
        // var_dump($clustersID['cluster']);
        echo $clusterString;
        echo "</pre>";
        $stageId=$this->input->post('property_status');
        print_r($clustersID);
        $data['properties'] = $this->p->getPropertyByAreaCluster($propertyType, $area, $clustersID, $stageId);

        //$data['props'] = $this->p->PropView($propId);
        $this->load->view('template/header', $data);
        $this->load->view('Property/property_view', $data);
        $this->load->view('template/footer');
   }

答案 1 :(得分:0)

你可以试试这段代码:

<强>控制器

public function fetchdata(){
$condition_array = array('category.id' => '1');
                $data = '*';
                $result_category_list = $this->common->select_data_by_condition('category', $condition_array, $data, $sortby = 'category_name', $orderby = 'ASC', $limit = '', $offset = '', $join_str = array());
}

<强>模型

function select_data_by_condition($tablename, $condition_array = array(), $data = '*', $sortby = '', $orderby = '', $limit = '', $offset = '', $join_str = array()) {

        $this->db->select($data);
        $this->db->from($tablename);

        //if join_str array is not empty then implement the join query
        if (!empty($join_str)) {
            foreach ($join_str as $join) {
                if (!isset($join['join_type'])) {
                    $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id']);
                } else {
                    $this->db->join($join['table'], $join['join_table_id'] . '=' . $join['from_table_id'], $join['join_type']);
                }
            }
        }

        //condition array pass to where condition
        $this->db->where($condition_array);


        //Setting Limit for Paging
        if ($limit != '' && $offset == 0) {
            $this->db->limit($limit);
        } else if ($limit != '' && $offset != 0) {
            $this->db->limit($limit, $offset);
        }

        //order by query
        if ($sortby != '' && $orderby != '') {
            $this->db->order_by($sortby, $orderby);
        }

        $query = $this->db->get();

        //if limit is empty then returns total count
        if ($limit == '') {
            $query->num_rows();
        }
        //if limit is not empty then return result array
        log_message('debug', 'fetching data result:' . $this->db->last_query());
        return $query->result_array();
    }