I'm trying to pass as Array as parameter
to Stored Procedure
in my Code-Igniter
application. When I'm print that array it gives me correct result of selected values. But it gives me an error in my model function.
Here is my controller:
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');
}
Here is my Model:
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;
}
}
Error:
A PHP Error was encountered Severity: Notice
Message: Array to string conversion
Filename: models/Property_m.php
Line Number: 26
Backtrace:
File: /opt/lampp/htdocs/livemg/application/models/Property_m.php Line: 26 Function: _error_handler
File: /opt/lampp/htdocs/livemg/application/controllers/Property.php Line: 61 Function: getPropertyByAreaCluster
File: /opt/lampp/htdocs/livemg/index.php Line: 315 Function: require_once
A Database Error Occurred Error Number: 1054
Unknown column 'Array' in 'field list'
call fetch_propertyType_Area_Cluster_Stage(0,0,Array,0)
Filename: models/Property_m.php
Line Number: 26
I got an answer, just convert that array to string and pass it.
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');
}
you can try this code:
Controller
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());
}
Model
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();
}