How would you?.... Handle mutli-add form

Hey i have a question,

I am trying to figure out the best way to handle a form with many different rows. Right now what i have going on is there are some calculations done and the start/stop are determined by that. And i have a var being incremented by one in a while loop. Now this all works fine and dandy if all the id’s are numerical increments of each other. But i know in the ‘real’ world this well not be the case. See i have the information for the form pulled from a table only where one column is a match. So the id’s well not always be an increment of the previous match ( do you get what i mean?). So the way i was naming the inputs before ( having the id in the name) has to change. Or i need a better way to process what is returned by the form.

-C.

Or you just make your inputs into arrays and match up the values.
<input type=‘text’ name=‘name’ value=‘’><input type=‘text’ name=‘wark’>
<input type=‘text’ name=‘name’ value=‘’><input type=‘text’ name=‘wark’>

Doh!! <dumb>

Just be careful to use input types that will always exist (avoid checkboxes, for example) or use a keyed array.

Well here’s a problem. I have had people request a check box so they can simpley tick if it happened or not.

<input type=‘text’ name=‘myname[$key]’ value=‘’><input type=‘checkbox’ name=‘check[$key]’> …
indexes do not need to be sequential for a [FPHP]foreach[/FPHP] walk.

So then i could just use a foreach state meant to walk through it?

 foreach ( $_POST('myname[]') as $input) {
blah here 
}

would something like that work?

Also have the question how could i deal with multiple inputs, say one for the date, and a hidden one for the record. i assume the same way as the example you gave. But than how could i retrieve that data in the script that processes it (all said fields well have the same id)? unless i was able to set a variable, like $x with the current id and use that to pull out all the other info from the other arrays. Would that work?

Hey Guy’s!

I have made a script and it seems to work (i am sure there is a way for it to be simplified). The one issue i am having with it, is it is printing out everything(even if a box is not ticked) or too many (about 3x what is needed) see this example

INSERT INTO eggs(dateLaid,laidBy) VALUES ('2013-02-12','4'),('2013-02-12','4'),('2013-02-12','4'),('2013-02-12','4'),('2013-02-12','3'),('2013-02-12','3'),('2013-02-12','3'),('2013-02-12','3'),;

and here’s the script.

any advice would be wonderful!

$totalHens = post('henCount');
		$henId = post("id");
		$i = 0;
		$date = post("date");
		$laid = post("laid");


		$query ="INSERT INTO eggs(dateLaid,laidBy) VALUES ";

		foreach ($henId as $input ) {
				foreach ($laid as $hasLaid => $value) {
					if($value == 'laid'){
						foreach ($date as $theDate => $dateLaid) {
							$query .= "('{$dateLaid}','{$input}')";
								if($i < $totalHens){
									$query  .= ",";
								}
						}
					}
				}
			$i++;
		}

		$query .= ";";
		return $query; 

i had to do the foreach for laid, and date, or else it was not getting the correct data. As i said earler i am sure there is a way to make this cleaner. I just kind of hacked everything together

Use the keys.

Assuming you have two arrays:
$VarOne and $VarTwo

and the values of $VarOne correspond to the values of $VarTwo using their keys
(So $VarOne[0] corresponds to $VarTwo[0], if it exists. Note that this is a LEFT joining; every value in VarOne will be used; any values in VarTwo that have no key in VarOne will not be walked.)


$VarOne = array(
0 => "Moo",
1 => "Quack", //Note: This has no corresponding value in $VarTwo.
6 => "Splat"
);

$VarTwo = array(
0 => "Cow",
2 => "Chicken", //This has no value in $VarOne
6 => "Slime"
);

In order to walk and associate these values in a meaningful way, using all of the values in $VarOne:


foreach($VarOne AS $key => $sound) {
  echo "Sound: ".$sound.", Animal: ".$VarTwo[$key];
}

Result:


Sound: Moo Animal: Cow
Sound: Quack Animal:     (This would also generate an "Index not found" error)
Sound: Splat Animal: Slime