3

我正在制作一份食谱清单。用户可以输入多种成分,其中每种成分都有名称(西红柿、洋葱、牛奶)、数量(5 个西红柿、1 个洋葱)、测量值(盎司、汤匙、杯)和分数(如果需要)(1/4 杯、1 /2 汤匙,3/4 茶匙)。我想将每个变量保留为一个单独的变量,以便以后可以使用它来创建购物清单。

我可以使用 foreach 函数来完成这项工作吗?我如何能够为我可以使用的每种成分将四个变量设置为一个数组?

还是有另一种方法可以做到这一点?一个变量可能有多个值?

<?php
$recipe = $_POST["recipe"];
$ingredient_amount_01 = $_POST["ingredient_amount_01"];
$fractions_01 = $_POST["fractions_01"];
$measurement_01 = $_POST["measurement_01"];
$ingredient_01 = $_POST["ingredient_01"];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>

<html>
<head>
<title>Recipes</title>
</head>
<body>

<form method="post" action="<?php echo $PHP_SELF;?>">

Recipe Name:<input type="text" size="12" maxlength="12" name="recipe"><br />
Ingredients<br />

1:<select name="ingredient_amount_01">
<option value="">Amount...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select>

<select name="fractions_01">
<option value="">---</option>
<option value="one_quarter">¼</option>
<option value="one_half">½</option>
<option value="three_quarters">¾</option></select>

<select name="measurement_01">
<option value="">Measurement...</option>
<option value="cup">Cup</option>
<option value="oz">Oz</option>
<option value="scoop">Scoop</option>
<option value="slice">Slice</option>
<option value="tablespoon">Tbsp</option>
<option value="teaspoon">Tsp</option></select>

<input type="text" size="12" maxlength="100" name="ingredient_01" placeholder="Ingredient"><br />

<input type="submit" value="submit" name="submit">
</form>

<?
} else {
echo "Hello. Here is the recipe for <b>" .$recipe. "</b> that you've submitted.<br />";

echo "Ingredients:<br />";
foreach ($ingredient as $g) {
echo $g <-- This would show something like, 2 1/2 cup milk -->. "<br />";
}
echo "Recipe:".$recipe."<br />";
echo "Item Amount:".$ingredient_amount_01."<br />";
echo "Fractions:".$fractions."<br />";
echo "Measurement:".$measurement."<br />";
echo "Ingredient:".$ingredient."<br />";
}
?>
4

2 回答 2

3

您可能希望这样命名您的字段:

<select name='ingredient[]'> <!--options here --> </select>
<select name='fraction[]'><!-- fraction options here --></select> 
etc. 

这将在您发布数据时将值加载到数组中,然后您可以使用 for 循环遍历所有提交的字段。例如,

<?php 
$ingredients = $_POST['ingredients']; //creates an array of all posted ingredients
$fractions = $_POST['fractions']; //creates an array of all posted fractions

for ($i = 0; $i < count($ingredients); $i++)
{
     $all_ingredients[] = $ingredient[$i] . ' ' . $fractions[$i]; 
}

?>

假设第一种成分是西红柿,分数是 1/4,回显 $all_ingredients[0] 将显示:tomato 1/4 您可以对此进行扩展以包含所有表单字段并根据需要输出字符串。

于 2012-09-24T22:02:38.473 回答
0

改变这个:

$recipe = $_POST["recipe"];
$ingredient_amount_01 = $_POST["ingredient_amount_01"];
$fractions_01 = $_POST["fractions_01"];
$measurement_01 = $_POST["measurement_01"];
$ingredient_01 = $_POST["ingredient_01"];

有了这个:

$ingredient['recipe'] = $_POST["recipe"];
$ingredient['amount_01'] = $_POST["ingredient_amount_01"];
...

在您的 foreach 中,您可以使用:

foreach($ingredient as $key=>$value)
   echo $key.":".$value;
于 2012-09-24T22:02:28.053 回答