-1

我有一个表格供用户使用我们数据库中的食谱创建每日膳食计划。流程如下:

1) 管理员为所需日期的每餐(午餐和/或晚餐)选择 2 个食谱。这些选择将用于填充客户膳食选择列表。

2) 客户为所需日期的每餐选择 1 个食谱。

问题:

该表单似乎只处理从页面上显示的最后一个列表中选择的值,我不知道为什么。

这是我一直在使用的代码部分。(有需要我可以发更多)

// 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&amp;dosql=update&amp;view=daily&amp;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>
4

1 回答 1

1

午餐和晚餐代码部分使用相同的name属性值。您需要给它们完全不同的名称或追加[]以创建一个数组。

编辑:例如,

echo '<input type="checkbox" name="delete_'.$i.'[]" value="yes"></td>';

注意属性[]值的双引号内。name你需要对他们所有人都这样做。我不熟悉,DBUtils::arrayselect所以我不确定以下是否可行。

echo DBUtils::arrayselect($mealList, 'meal_id_'.$i.'[]', 'size=1', $meal);

但只要您有多个同名字段,它们就会相互覆盖。通过将这些字段制作成数组,您还需要更改处理表单的代码,以便它可以处理数组。

编辑:您也可以在午餐和晚餐部分之间增加 $i 而不是创建数组。在循环中增加 $i 需要更改for参数,但至少您不必更改处理表单的代码。

for条线对我来说似乎很复杂(而且效率低下)。如果单行代码在循环的每次迭代中运行不必要的计算,它的效率并不高。如果我正确理解了意图,请尝试用for以下内容替换该行。

$maxshow = 1;
$num_items = isset($MPObj->mealplanItems[$dbDate]) ? count($MPObj->mealplanItems[$dbDate]) : 0;
if ($num_items + $minshow < $maxshow) $maxshow = $num_items + $minshow;
$maxshow = $maxshow * 2; //double it because we will be incrementing $i within the loop

for ($i = 0; $i < $maxshow; $i++) {

然后在您原来的午餐和晚餐部分(没有的部分)之间添加以下行[]

$i++;

这有效地为每个字段分配了不同的名称,而无需创建数组。

于 2012-11-30T11:05:54.323 回答