【问题标题】:Passing an Array as Parameter to Model in PHP CodeIgniter在 PHP CodeIgniter 中将数组作为参数传递给模型
【发布时间】:2018-02-27 04:27:04
【问题描述】:

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

这是我的控制器:

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

错误:

遇到 PHP 错误 严重性:通知

消息:数组到字符串的转换

文件名: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 功能: 需要一次

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

“字段列表”中的未知列“数组”

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

文件名:models/Property_m.php

行号:26

【问题讨论】:

  • 尝试使用 var_dump() 将 clusterId 打印到函数中
  • 在哪里?在控制器或模型函数中。
  • 进入模型函数
  • 感谢您的回复,得到答复。
  • 欢迎。好.. :)

标签: php codeigniter parameters codeigniter-3


【解决方案1】:

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

    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');
   }

【讨论】:

    【解决方案2】:

    你可以试试这个代码:

    控制器

    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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-06
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 2017-01-19
      相关资源
      最近更新 更多