I want to insert multiple rows into the db using submit button, This is my view code
<?php foreach($role_array as $k =>$v) { //var_dump($k); if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" ) { ?> <div class="row-fluid"> <div class="span6"> <div class='control-group'> <label for='<?php echo $k; ?>' class='control-label'><?php echo $v ;?></label> <div class='controls'> <select class='form-control' name='<?php echo $k; ?>[]' id='<?php echo $k; ?>' multiple='multiple' size='5'> <?php foreach($all_datas[$k] as $datas ) { $username = "{$datas->nickname}"; echo "<option value='{$datas->id}'>{$username}</option>"; } ?> </select> <label for="<?php echo $k; ?>" class="error" style="display:none"></label> </div> </div> </div> </div> <?php }} ?> This is my controller code
$roles = $this->utility->roles; foreach($roles as $k => $v) { if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k])) { $userrole = $k; $post_user=isset($post_data[$k])?$post_data[$k]:''; if (count($post_user) > 0) { for ($i = 0; $i < count($post_user); $i++) { $data = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]); // var_dump($data); $flag = true; } } } } if(isset($flag)) { $v=$this->JournalUsers->addJournalUser($data); } $data output is
array(3) { ["journal_id"]=>int(83) ["user_role"]=> string(15) "quality_checker" ["user_id"]=> string(3) "136" } array(3) { ["journal_id"]=> int(83) ["user_role"]=> string(5) "texer" ["user_id"]=> string(3) "103" } array(3) { ["journal_id"]=> int(83) ["user_role"]=> string(5) "texer" ["user_id"]=> string(3) "133" } array(3) { ["journal_id"]=> int(83) ["user_role"]=> string(5) "texer" ["user_id"]=> string(3) "132" } array(3) { ["journal_id"]=> int(83) ["user_role"]=> string(10) "typesetter" ["user_id"]=> string(3) "109" } array(3) { ["journal_id"]=> int(83) ["user_role"]=> string(10) "typesetter" ["user_id"]=> string(3) "153" } But,when insert the data only one row will be inserted on the table.
array(3) { ["journal_id"]=> int(83) ["user_role"]=>`enter code here` string(10) "typesetter" ["user_id"]=> string(3) "153" } I want to insert all datas in db