Reputation: 57
I have array with checkboxes values, and id value. Checkboxes can have many values, otherwise id is only one. My array is $data2
, print_r:
Array (
[checkbox] => Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 8
[5] => 9
[6] => 10
[7] => 21
)
[id] => 8
)
Code in model is
function checkboxes($data2)
{
foreach($data2->result() as $row)
{
$data= array(
'story'=> $row->checkbox,
'id'=>$row->id
);
$this->db->insert('stories_to_categories',$data);
}
}
Problem is because i have array in array can someone help me to insert this code into db?
also i wanna have same number ids and checkboxes inserted, for example 3 id with value 1 and 3 checkboxes values
Upvotes: 0
Views: 456
Reputation: 1605
Here is the Dummy answer which will clear your concept that how multidimensional arrays workout in the codeigniter first of all when you are working with multidimensional so you need a key for tracking up your record which record belong to which key so it can be helpful for fetching values so far i have created a simple dummy program specially for you and have also worked in your code as well so you might like my effort as well as i hope your problem will get solved .Also check out the php manual and documentation which will make you more clear about multidimensional arrays Multidimensional array documentation as well as i have use function sizeof()
this function is use for to measure the size of an object either its self created or database object it help in measurement here is the documentation link follow along to check how can we use that sizeof documentation.
Here is the dummy code.
$section = array( 'Clients'=>array('pepsi','cocacola','sevenup'),
'Services'=>array('Web Design','Graphic Design','DataMigration'),
'Techonologies'=>array('codeigniter','postgresql','ubuntu')
);
$i=0;
foreach($section as $key => $row)
{
//taking up specific value
echo $section['Techonologies'][0];
echo '<pre>';
//checking the size of the key in array Clients is a key and it has 3 values
while($i<sizeof($row))
{
echo $row[$i].'<br/>';
$i++;
}
$i=0;
echo '</pre>';
}
echo '<pre>';
print_r($section);
echo '</pre>';
}
Here is your code i hope it may help you out from the trouble .
function checkboxes($data2)
{
$i=0;
$data=$data2->result();
foreach($data as $key => $row)
{
while($i<sizeof($row))
{
$data= array(
'story'=> $row[$i],
'id'=>$data['id'][0]
);
$this->db->insert('stories_to_categories',$data);
$i++;
}
}
}
Upvotes: 1