yulianto saparudin yulianto saparudin - 1 year ago 64
PHP Question

Insert multiple data separated with comma in Codeigniter

I wanna ask how to insert multiple data with comma, I have a data like this :

$fruits = 'apple, banana, grape';
$beverages = 'coffee, tea, juice';


then I wan to insert the data in one action

here my code

$arr = explode(',', $fruits);
for($i = 0; $i < count($arr); $i++)
{
$item = array(
'fruits' => $fruits[$i],
'beverages' => $beverages[$i],
);

$this->db->insert('tb_menu', $item);
}


it's not working, then I use print_r the data displaying like this

Array ( [fruits] => a [beverages] => c ) Array ( [fruits] => p [beverages] => o ) Array ( [fruits] => p [beverages] => f )


anyone know how to handle this? Plz help me!!

Answer Source

Here is your code.

$fruits = 'apple, banana, grape';
$beverages = 'coffee, tea, juice';
$fruitsArray = explode(',', $fruits);
$beveragesArray = explode(',', $beverages);

for($i = 0; $i < count($fruitsArray); $i++)
{

    $item = array('fruits' => $fruitsArray[$i],'beverages' => $beveragesArray[$i]);
    $this->db->insert('tb_menu', $item);

}

try this may it helps you. one thing both string contain no of comma separated value same. otherwise there may be chance to skip for some values.

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