Solarpitch Posted March 30, 2010 Share Posted March 30, 2010 Hey Guys, I have an input array that I need to loop through on form submission then write to the database. Using Codeigniter.. The below inputs child age and month are dynamic so there can be 10 generated by javascript if required.. below we have 2... HTML.. <h5>Age of Children</h5> <input type="text" name="child[att][age]" value="" size="50" /> <select name="child[att][month]"> <option value="Jan">Jan</option> <option value="Feb">Feb</option> <option value="Mar">Mar</option> </select> <input type="text" name="child[att][age]" value="" size="50" /> <select name="child[att][month]"> <option value="Jan">Jan</option> <option value="Feb">Feb</option> <option value="Mar">Mar</option> </select> Then I'm just trying to loop through and save... foreach($_POST['child']['att'] as $key => $val) { // print_r($_POST['child']['att']); -> THIS PRINTS: Array ( [age] => Array ( [0] => 5 [1] => 4 ) [month] => Array ( [0] => Feb [1] => Jan ) ) $insert = array( 'entry_id' => $last_id_inserted, 'age_of_child' => $val['age'], 'child_month' => $val['month'], 'date_created' => date('Y-m-d H:i:s'), 'last_modified' => date('Y-m-d H:i:s') ); $this->competition_model->add_entry_children($insert); } I cant get the values into the variables. So you can see I need to make two DB inserts foreach of the child age and child month thats submitted. As stated above, print_r($_POST['child']['att']); -> THIS PRINTS: Array ( [age] => Array ( [0] => 5 [1] => 4 ) [month] => Array ( [0] => Feb [1] => Jan ) ) Any help appriciated. Quote Link to comment https://forums.phpfreaks.com/topic/196969-loop-through-an-array-on-form-input/ Share on other sites More sharing options...
trq Posted March 30, 2010 Share Posted March 30, 2010 As print_r clearly points out, $_POST['child']['att']['age'] & $_POST['child']['att']['month'] are both still arrays. You need to loop through those arrays as well. Quote Link to comment https://forums.phpfreaks.com/topic/196969-loop-through-an-array-on-form-input/#findComment-1034031 Share on other sites More sharing options...
Solarpitch Posted March 30, 2010 Author Share Posted March 30, 2010 Umm.. If I try this.. foreach($_POST['child']['att'] as $key => $val) { foreach($_POST['child']['att']['age'] as $key => $val){ echo print_r($_POST['child']['att']['month']); } //print_r($_POST['child']['att']); exit; $insert = array( 'entry_id' => $last_id_inserted, 'age_of_child' => $val['age'], 'date_created' => date('Y-m-d H:i:s'), 'last_modified' => date('Y-m-d H:i:s') ); $this->competition_model->add_entry_children($insert); } the out put is.. Array ( [0] => Feb [1] => Jan ) 1Array ( [0] => Feb [1] => Jan ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/196969-loop-through-an-array-on-form-input/#findComment-1034039 Share on other sites More sharing options...
trq Posted March 30, 2010 Share Posted March 30, 2010 You already define $key and $val in your outer loop. You'll need to use different variables within the inner loop. Quote Link to comment https://forums.phpfreaks.com/topic/196969-loop-through-an-array-on-form-input/#findComment-1034041 Share on other sites More sharing options...
Solarpitch Posted March 30, 2010 Author Share Posted March 30, 2010 Ah right Thorpe I see.. So would this be best practice... foreach($_POST['child']['att'] as $key => $val) { foreach($_POST['child']['att']['age'] as $key_1 => $val_1){ foreach($_POST['child']['att']['month'] as $key_2 => $val_2){ //print_r($_POST['child']['att']); $insert = array( 'entry_id' => $last_id_inserted, 'age_of_child' => $val_1, 'birth_month' => $val_2, 'date_created' => date('Y-m-d H:i:s'), 'last_modified' => date('Y-m-d H:i:s') ); $this->competition_model->add_entry_children($insert); } } } Quote Link to comment https://forums.phpfreaks.com/topic/196969-loop-through-an-array-on-form-input/#findComment-1034043 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.