我正在制作一份食谱清单。用户可以输入多种成分,其中每种成分都有名称(西红柿、洋葱、牛奶)、数量(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 />";
}
?>