...

View Full Version : PHP Programming brain fart



metomeya
02-26-2010, 10:49 PM
Okay, I'm trying to figure out the best way to do this.

I have a meal plan generated for the week. Now I'm trying to produce a grocery list to go with it.

My recipeingredients table is structured

IngAMT | UnitID | IngreID,

I want to add up all the ingredients (since some meals will use the same ingredients).

For example:


Meal1 Ingredients:
2 tsp baking soda

meal2 ingredients:
3 tsp baking soda


Grocery List:
5 tsp baking soda

I think the way I structured to code to generate the meal plan is giving me the hardest time to figure out how to do it. Here is the code...



<?php session_start();
include("code/connect.php");

$long_list = "";
$short_list = "";

//////////////////////////////////////////MTWTRFSS///////////////////////////////
$sql0 = "SELECT * FROM day_of_week";
$result0 = mysql_query($sql0);
while($row0 = mysql_fetch_array($result0))
{
$day=$row0["Day"];
$long_list .= "<b><u><h3>" . $day . "</h3></b></u></p> <br>";
$short_list .= "<b><u><h3>" . $day . "</h3></b></u> <br>";

/////////Random Breakfast/////////////
$sql = "SELECT * FROM meals
WHERE typeID ='1'
ORDER BY RAND() LIMIT 1";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$Breakfast_ID = $row['MealID'];
$Breakfast_Name = $row['mealName'];
$Breakfast_Desc = $row['RecDesc'];
}

$sql = "SELECT * FROM recipeingredients
WHERE MealID ='$Breakfast_ID'";
$result=mysql_query($sql);
$Breakfast_ingre_list = "";
while ($row=mysql_fetch_array($result))
{
$Amt=$row["IngreAmt"];
$UnitID=$row["UnitID"];
$IngreID=$row["IngreID"];
$sql2 = "SELECT * FROM unit
WHERE UnitID ='$UnitID'";
$unitresult = mysql_query($sql2) or die("Query Error: ".mysql_error());
while($unit_row = mysql_fetch_array($unitresult))
{
$UnitName = $unit_row['UnitName'];
}
$sql_3 = "SELECT * FROM ingredients
WHERE IngreID ='$IngreID'";
$result_ingre=mysql_query($sql_3);
while($ingre_row = mysql_fetch_array($result_ingre))
{
$IngreName = $ingre_row['IngreName'];
}

$Breakfast_ingre_list.= $Amt . " " . $UnitName . " " . $IngreName . "<br>";
}

$long_list .= "<b><u>Breakfast</u></b> <br><br>";
$long_list .= "<b>" . $Breakfast_Name . "</b> <br><br>";
$long_list .= $Breakfast_ingre_list;
$long_list .= "<br>";
$long_list .= $Breakfast_Desc . '<br><br>';

$short_list .= "<b><u>Breakfast</u></b> <br> " . $Breakfast_Name . ' <br><br>';

......................similar code reiterated for each meal Lunch, Dinner, etc.

}

echo $long_list;
echo $short_list;

$_SESSION['longlist'] = $long_list;
$_SESSION['shortlist'] = $short_list;

?>

metomeya
02-27-2010, 12:28 AM
No replies?

I'm thinking maybe I need to make an array, something like...

$grocery_list[$Unit][$IngreID] = $Amt

I'm just wondering how I'm going to pull everything out to write the grocery list

JAY6390
02-27-2010, 02:20 AM
If you use the GROUP BY in your query, you can actually get the amounts for each ingredient with your query. Something along the lines of

SELECT
SUM(IngAMT) as total_amt,
UnitID,
IngreID
FROM
recipeingredients
GROUP BY
UnitID,
IngreID

You might need to swap the IngreID and UnitID at the end to get it to work right

metomeya
02-27-2010, 07:29 PM
Hmm... I think that works, but I did a work around

I had an while loop for the column and row, and link that to the array.

Here is the (terrible) code I wrote. Of course I'll replace the 250 and 400 with a variable the represents the max of $UnitID and $NameID.



for ($row = 0; $row < 250; $row++)
{
for ($col = 0; $col < 400; $col++)
{
if (isset($grocery_list[$row][$col]))
{
$total_grocery_list .= "<br>";
$total_grocery_list .= $grocery_list[$row][$col] . " ";
$total_grocery_list .= $row . " ";
$total_grocery_list .= $col . "<br> ";

$sql = "SELECT * FROM unit
WHERE UnitID ='$row'";
$result=mysql_query($sql);
while ($find=mysql_fetch_array($result))
{
$UnitName=$find["UnitName"];
}
$sql = "SELECT * FROM ingredients
WHERE IngreID ='$col'";
$result=mysql_query($sql);
while ($find=mysql_fetch_array($result))
{
$IngreName=$find["IngreName"];
}
$total_grocery_list .= $grocery_list[$row][$col] . " " . $UnitName . " " . $IngreName . "<br>";
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum