user3551487 user3551487 - 4 years ago 604
PHP Question

send array from controller to model in codeigniter

Hello i need help i am trying to pass three different variables from the controller of a codeigniter to model so i created an array and pass it then i exploded and i am getting the error explode() expects parameter 2 to be string, array given

The controller

$data = array(
'district_id' => $this->input->post('district') ,
'limit' => $limit,
'offset' => $offset,

);


$data['tubadili'] = $this->wapi_db->search_bar($data);


The Model function

public function search_bar($data){

$dataArray = explode(',' , $data);

$district_id = $dataArray[0];

$limit = $dataArray[1];

$offset = $dataArray[2];

$this->db->select('entertainment.Name,entertainment.ID,entertainment.Category,entertainment.Location,entertainment.Description,
image.ImagePath,image.Enter_ID,entertainment.DistrictID');

$this->db->from('entertainment');

$this->db->join('image', 'image.Enter_ID=entertainment.ID');

$this->db->where('entertainment.Category',"Bar");

$this->db->where('entertainment.DistrictID', $district_id);


$this->db->limit($limit, $offset);

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

return $query->result();








}

Answer Source

Change:

$dataArray = explode(',' , $data);
$district_id = $dataArray[0];
$limit = $dataArray[1];
$offset = $dataArray[2];

To:

extract($data);

Basically what this does is take the associative array you have in $data and extracts them into their own variables, which is logically equal to:

$district_id = $data['district_id'];
$limit = $data['limit'];
$offset = $data['offset'];

Your issue is that you were trying to explode $data which will not work, explode works on strings and not arrays.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download