将数组作为参数传递给PHP CodeIgniter中的模型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数组作为参数传递给PHP CodeIgniter中的模型相关的知识,希望对你有一定的参考价值。
我想在我的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;
}
}
错误:
遇到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功能:require_once
发生数据库错误错误号:1054
“字段列表”中的未知列“数组”
call fetch_propertyType_Area_Cluster_Stage(0,0,Array,0)
文件名:models / Property_m.php
行号:26
我得到了一个答案,只需将该数组转换为字符串并传递它。
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');
}
你可以试试这段代码:
调节器
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();
}
以上是关于将数组作为参数传递给PHP CodeIgniter中的模型的主要内容,如果未能解决你的问题,请参考以下文章
如何将变量作为 cURL 数组中的 url 参数传递给 CURLOPT_URL