Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/196969-loop-through-an-array-on-form-input/
Share on other sites

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

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);


			}
		    }	
		}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.