I am trying to insert two arrays (associative) with foreach loop in codeigniter
$senti = $this->input->post('field_id');
$options = $this->input->post('field');
$i=0;
foreach( $options as $option and $senti as $sen )
{
$insert_option = array
(
'form_id' => $token,
'name' => $this->db->escape_str($option['name']),
'fillup_id' => $rand,
'field_id' => $this->db->escape_str($sen['id'])
);
$this->db->insert('form_value', $insert_option);
$i++;
}
But I get error with the above code.
On the other hand if I use foreach( $options as $option)
then I get the entries for $option['name']
but not for $sen['id']
.
Also if this could be done with any other loop I am happy to learn.
My Answer:
>input->post('sentiments');
$emotion = $this->input->post('emotions');
foreach($senti as $key => $value){
$data = array(
'sentiment' => $value
);
$this->db->insert('sentiments_table', $data);
}
foreach($emotion as $key => $value){
$data = array(
'emotion' => $value
);
$this->db->insert('emotions_table', $data);
}
This code snippet assumes that you have a 'sentiments_table' and 'emotions_table' in your database with columns 'sentiment' and 'emotion' respectively. It loops through the associative arrays $senti and $emotion, inserts each value into the corresponding table.
Rate this post
5 of 5 based on 8769 votesComments