Jump to content

Loop through an array on form input?


Solarpitch

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


			}
		    }	
		}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.