I'm working on a recipe inventory list. Users would be able to input multiple ingredients where each ingredient has a name (tomato, onion, milk), amount (5 tomatos, 1 onion), measurement (oz, tbsp, cup) and fractions if required (1/4 cup, 1/2 tbsp, 3/4 tsp). I want to keep each as a separate variable so that I can later use that to create a grocery list.
Would I be able to use the foreach function to make this work? How might I be able to set up four variables into an array for each ingredient that I can use?
Or is there an alternate way of doing this? Multple values for one variable maybe?
<?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 />";
}
?>