3

私はレシピ在庫リストに取り組んでいます。ユーザーは複数の材料を入力でき、各材料には名前(トマト、タマネギ、牛乳)、(トマト 5 個、タマネギ 1 個)、測定値(オンス、大さじ、カップ)、必要に応じて分数(1/4 カップ、1個) が含まれます。 /大さじ2、小さじ3/4)。後でそれを使用して食料品リストを作成できるように、それぞれを個別の変数として保持したいと考えています。

foreach 関数を使用してこれを機能させることはできますか? 使用できる材料ごとに 4 つの変数を配列に設定するにはどうすればよいでしょうか?

または、これを行う別の方法はありますか?1 つの変数に複数の値があるのでしょうか?

<?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]をエコーすると次のようになります。トマト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 に答える