Reputation: 31
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 />";
}
?>
Upvotes: 3
Views: 1979
Reputation: 1738
You may want to name your fields like this:
<select name='ingredient[]'> <!--options here --> </select>
<select name='fraction[]'><!-- fraction options here --></select>
etc.
This will load the values into an array when you post the data, and then you can use a for loop to iterate over all the submitted fields. For example,
<?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];
}
?>
assuming the first ingredient was tomatoes and the fraction was 1/4, echoing $all_ingredients[0] would show: tomato 1/4 You can expand on this to include all your form fields and ouput the string however you want.
Upvotes: 3
Reputation: 615
Change this :
$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"];
With this:
$ingredient['recipe'] = $_POST["recipe"];
$ingredient['amount_01'] = $_POST["ingredient_amount_01"];
...
In your foreach you can use :
foreach($ingredient as $key=>$value)
echo $key.":".$value;
Upvotes: 0