I have a form for users to create a daily mealplan using recipes from our database. The processes are as follows:
1) An admin selects 2 recipes for each meal (Lunch and/or Dinner), for the desired date. These selections are what will be used to populate the customer meal select lists.
2) A customer selects 1 recipe for each meal available, for the desired date.
The PROBLEM:
The form only seems to be processing the values selected from the last list displayed on the page and I am not sure why.
Here’s the code sections I’ve been working with. (I can post more if necessary)
// Create a new Meal Plan object
$MPObj = new MealPlan($date);
$MPObj->load($dbDate,$dbDate); // Just want this one day
$minshow = 1;
$defaultServings = 1;
// Read in a list of Meals and Recipes
$rc = DBUtils::fetchColumn( $db_table_meals, 'meal_name', 'meal_id', 'meal_id' );
$mealList = DBUtils::createList($rc, 'meal_id', 'meal_name');
array_unshift_assoc($mealList, 0, ""); // add an empty element to the list
—
$sql = "SELECT mplan_date,
mplan_recipe,
recipe_name,
meal_name,
recipe_serving_size
FROM recipe_mealplans
LEFT JOIN $db_table_meals ON meal_id = mplan_meal
LEFT JOIN $db_table_recipes ON recipe_id = mplan_recipe
WHERE mplan_date = '".mysql_real_escape_string($dbDate)."'
AND mplan_owner = '".mysql_real_escape_string($user_SK)."'
ORDER BY recipe_name";
$rc = $DB_LINK->Execute($sql);
DBUtils::checkResult($rc, NULL, NULL, $sql);
—
while (!$rc->EOF) {
if ($rc->fields['meal_name'] === "Lunch") {
$recipeListLunch[($rc->fields['mplan_recipe'])] = $rc->fields['recipe_name'] . " (" . $rc->fields['recipe_serving_size'] . ")";
}
if ($rc->fields['meal_name'] === "Dinner") {
$recipeListDinner[($rc->fields['mplan_recipe'])] = $rc->fields['recipe_name'] . " (" . $rc->fields['recipe_serving_size'] . ")";
}
$rc->MoveNext();
}
—
<form action="index.php?m=meals&dosql=update&view=daily&date=<?php echo $date; ?>" method="post" enctype="multipart/form-data">
<table cellspacing="1" cellpadding="4" border="0" width="80%" class="data">
<tr>
<th align="center">Remove</th>
<th align="center">Meal</th>
<th align="center">Servings</th>
<th align="center"></th>
<th align="center">Menu Options</th>
</tr>
<?php
// Print out all the existing meals, and some new ones
for ($i = 0, $maxshow = 1; $i < (isset($MPObj->mealplanItems[$dbDate]) && ($i < $maxshow) ? count($MPObj->mealplanItems[$dbDate]) : 0) + $minshow; $i++) {
if ($i < (isset($MPObj->mealplanItems[$dbDate]) ? count($MPObj->mealplanItems[$dbDate]) : 0)) {
// If it is an existing meal item, then set it
$meal = $MPObj->mealplanItems[$dbDate][$i]['meal'];
$servings = $MPObj->mealplanItems[$dbDate][$i]['servings'];
$recipe = $MPObj->mealplanItems[$dbDate][$i]['id'];
} else {
// It is a new one, give it blank values
$meal = NULL;
$servings = $defaultServings;
$recipe = NULL;
}
/* Display Meal Lists to select from */
// Lunch
echo '<tr>';
echo '<td align="center">';
echo '<input type="checkbox" name="delete_'.$i.'" value="yes"></td>';
echo '<td align="center">';
echo DBUtils::arrayselect($mealList, 'meal_id_'.$i, 'size=1', $meal);
echo '</td><td align="center">';
echo '<input type="text" autocomplete="off" name="servings_'.$i.'" value="' . $servings . '" size="3">';
echo '</td><td align="center">';
echo '<input type="hidden" autocomplete="off" name="repeat_'.$i.'" value="1" size="3"> ';
echo '</td><td align="center">';
echo DBUtils::arrayselect($recipeListLunch, 'recipe_id_'.$i, 'size=1', $recipe);
echo '</td></tr>';
// Dinner
echo '<tr>';
echo '<td align="center">';
echo '<input type="checkbox" name="delete_'.$i.'" value="yes"></td>';
echo '<td align="center">';
echo DBUtils::arrayselect($mealList, 'meal_id_'.$i, 'size=1', $meal);
echo '</td><td align="center">';
echo '<input type="text" autocomplete="off" name="servings_'.$i.'" value="' . $servings . '" size="3">';
echo '</td><td align="center">';
echo '<input type="hidden" autocomplete="off" name="repeat_'.$i.'" value="1" size="3"> ';
echo '</td><td align="center">';
echo DBUtils::arrayselect($recipeListDinner, 'recipe_id_'.$i, 'size=1', $recipe);
echo '</td></tr>';
} // end for
?>
</table>
<?php
if isset($recipeListLunch) || isset($recipeListDinner)) {
echo '<input type="submit" value="Update Menu" class="button">';
}
?>
</form>
The lunch and dinner code sections are using the same values for the
nameattributes. You need to give them different names altogether or append[]to create an array.EDIT: For example,
Notice the
[]inside the double quotes for the value of thenameattribute. You’d need to do this to all of them. I am not familiar withDBUtils::arrayselectso I am not if sure the following will work.But as long as you have more than one field with the same name, they will overwrite each other. With making these fields into arrays, you also need to change the code that processes the form so that it can process an array.
EDIT: You could also increment $i between the lunch and dinner sections instead of creating an array. Incrementing $i within the loop would require changing the
forparameters, but at least you wouldn’t have to change the code that processes the form.The
forline seems very convoluted to me (and inefficient). A single line of code is not more efficient if it runs unnecessary calculations on each iteration of a loop. If I understand the intention correctly, try replacing theforline with the following.And then add the following line between your original lunch and dinner sections (the ones without
[]).That effectively assigns different names to each field without creating arrays.