Hi Guys,
I have a nested loop that just won't behave the way I want it to.
I use a nested for to dynamically create a form with x number of category title textfields for people to fill out and x number of category items pr. category for people to fill out. It looks like this:
//
//picking up the integers where the user origuinally types them in
//
$optionsets = (isset($_POST['nr_of_option_sets']) AND ctype_digit($_POST['nr_of_option_sets']) AND $_POST['nr_of_option_sets'] < 11) ? trim($_POST['nr_of_option_sets']) : 0;
$options = (isset($_POST['nr_of_options']) AND ctype_digit($_POST['nr_of_options']) AND $_POST['nr_of_options'] < 21) ? trim($_POST['nr_of_options']) : 0;
//
// Saving the two picked up parameters to also be used by the processing script
//
<p><input name='optioncounter' value='$options' type='hidden' size='2' maxlength='2' /></p>;
<p><input name='optionsetcounter' value='$optionsets' type='hidden' size='2' maxlength='2' /></p>;
for ($os = 1; $os <= $optionsets; ++$os) {
echo "<p><label class='dynamic_label'>Set Title:</label><input class='dynamic_input' name='option_title$os' type='text' size='20' maxlength='80' /></p><br /><br />";
for ($o = 1; $o <= $options; ++$o) {
echo "<label class='dynamic_label'>Option:</label><input class='dynamic_input' name='option_name$o' type='text' size='5' maxlength='80' />
<label class='dynamic_label'>Stock:</label><input class='dynamic_input' name='stock$o' type='text' size='5' maxlength='10' />
<label class='dynamic_label'>Price:</label><input class='dynamic_input' name='price$o' type='text' size='5' maxlength='10' /><br />";
}
echo "<br />";
}
}
I then try to pick them up in a processing scrip using the same parameters ($optionsets and $options)
//
// Pulling the two parameters from the previous page, to be used to pick up and process the fields.
//
$option_set_counter = $_POST['optionsetcounter'];
$option_counter = $_POST['optioncounter'];
//
//picking up the category titles and their respective rows named with the loop integer attached to the fieldnames.
//
for ($osc = 1; $osc <= $option_set_counter; ++$osc)
{
$getoptiontitles = strip_tags(mysql_real_escape_string($_POST["option_title$osc"]));
echo $getoptiontitles;
echo "<br />";
for ($oc = 1; $oc <= $option_counter; ++$oc)
{
// echo $option_counter;
echo $getoptionname = strip_tags(mysql_real_escape_string($_POST["option_name$oc"]));
echo $getoptionstock = strip_tags(mysql_real_escape_string($_POST["stock$oc"]));
echo $getoptionprice = strip_tags(mysql_real_escape_string($_POST["price$oc"]));
echo "<br /><br />";
}
}
}
- and for some reason, the inner loop in code chunk two fails to pick up anthing but the fields connected to it's last iteration. So when I am expecting: Alpha xxx Beta yyy Gamma zzz, I get: Alpha zzz Beta zzz Gamma zzz.
Why doesn't it pull the fields with the x'es and the y's?
The answer may be obvious, but I've stared myself blind at this long ago.:glare:
Hope you can help.