I am currently trying to put a cookbook together that holds a collection of recipes. This is purely to try to help me improve my knowledge of mysql/php.
Currently my issue is, I have the output but within my while loop I have a title, sub title and paragraph and then the ingredients for said recipe however I because I have the title etc inside the while loop it then outputs for every ingredient so I have title, sub title, paragraph, ingredient then title and so on.
$recipe4 = "SELECT * FROM recipes
JOIN recipe_ingredients ON recipes.recipe_id = recipe_ingredients.recipe_id
JOIN measurement_qty ON recipe_ingredients.measurement_quantity_id = measurement_qty.measurement_qty_id
JOIN measurement_units ON recipe_ingredients.measurement_id = measurement_units.measurement_id
JOIN ingredients ON recipe_ingredients.ingredient_id = ingredients.ingredient_id
";
$stmt = $pdo->query($recipe4);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<h1>".$row['recipe_name']."</h1>";
echo "<em>".$row['recipe_description']."</em>";
echo "<p>".$row['qty_amount']." ".$row['measurement_description']." ".$row['ingredient_name']."</p><br>";
}
You need to pre-fetch all the data into a php variable (see the PDO fetchAll() method.) You can then access the zeroth element to get the unique/one-time information for display.
Next, you should list out the columns you are SELECTing. This helps avoid mistakes, especially with JOINed queries, and it makes your code self-documenting (anyone can see what you are trying to do without needing the database table definitions.)
Also, (PDO::FETCH_ASSOC), if you set the default fetch mode to assoc when you make the database connection, you don’t need to specify it in each fetch statement.
Lastly, that query will match all recipes. If that’s your intent, you would want to index/pivot the data using the recipe id as the main array index, when you fetch the data. This will give you a sub-array for each recipe, that you can loop over to output the data for each individual recipe. There’s actually a fetch mode that will index the data for you.
Did you look at the documentation for the fetchAll() method?
Since we don’t know what you specifically changed to produce that value in $row or what the code leading up to line 332 is, I’m not sure what help we can give you.
The fetchAll() method’s purpose is to fetch all the rows of data at once. You can then do whatever you want with that data, such as accessing the zeroth (1st row) element to get the recipe name and description, or looping over the data -
// get all the rows of data at once
$rows = $stmt->fetchAll();
// get a copy of the zeroth row
$heading = $rows[0];
echo "<h1>{$heading['recipe_name']}</h1>";
echo "<em>{$heading['recipe_description']}</em>";
foreach($rows as $row)
{
// access elements in $row to display the qty, measurement, and ingredient
}
I am having to physically go into the table to see what index the next menu title is.
Would the solution be to use DISTINCT in my query so that I only get 1 instance of each title rather than 1 title then 1 ingredient then same title then next ingredient or is there a better way of doing this?
Something along the lines of
$recipe = "SELECT DISTINCT recipe_name, recipe_description FROM recipes
JOIN recipe_ingredients ON recipes.recipe_id = recipe_ingredients.recipe_id
JOIN measurement_qty ON recipe_ingredients.measurement_quantity_id = measurement_qty.measurement_qty_id
JOIN measurement_units ON recipe_ingredients.measurement_id = measurement_units.measurement_id
JOIN ingredients ON recipe_ingredients.ingredient_id = ingredients.ingredient_id
";
Also wouldn’t a foreach/while loop be better than writting the following out for all the titles etc?
The fetch mode to add in the fetchAll(…) call is - PDO::FETCH_GROUP This will group the fetched data into sub-arrays, indexed by the first column you are SELECTing. You would typically use an id as this index. In your case, it would be the recipe_id.
After you do that, use print_r($rows) to see what the data looks like.
So, all you need to do is use two nested foreach() loops -
foreach($rows as $recipe_id=>$recipe)
{
$heading = $recipe[0];
// display the heading items here...
foreach($recipe as $row)
{
// display the individual ingredient data in $row here...
}
}