code:
$item_des = implode(',',$this->input->post('item_des'));
$item_cost = implode(',',$this->input->post('item_cost'));
$service_cost = implode(',',$this->input->post('service_cost'));
$total_cost = implode(',',$this->input->post('total_cost'));
$data = array(
'item_des' => $item_des,
'item_cost' => $item_cost,
'service_tax' => $service_cost,
'total_cost' => $total_cost,
);
print_r($data);
$query = $this->db->insert('table_name',$data);
In this code I have a form where I am using jquery/ajax for inserting form value but when I am using implode function it raise an error message and i.e.
Message: implode(): Invalid arguments passed
So, How can I fix this issue ?Please help me.
Thank You
You can convert $a to array to make sure you are always working with arrays when using implode
implode(',', (array) $a);
Where,
$a=$this->input->post('item_des');
and so on for the other cases. Just try this it will surely run !
try printing each and every variable
var_dump($this->input->post('name')); //all these should be array
most probably these fields aren't array. Do you have hidden fields? or your html should look like this:
<input name="item_des[]" /> <!-- name should be an array instead of a string name, as shown here -->
<input name="item_des[]" /> <!-- there could be multiple inputs for each of the array you want to `implode` -->
now HTML will return you an array in post. Please Implode() expects an array to convert it into a string.
This error means that your variables are not defined or not array. Before working with your data, make sure they are defined as you think they are.
You can use isset() to make sure your variables are defined and is_array() to make sure they are array. Once you know what your variables are, you can treat them the way they are supposed to be (maybe sending error of you are expecting a proper defined array).
Casting them into array won't help you since it means the data aren't what you excepted. The rest if your code will probably have issues further.