Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP Programming brain fart

    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 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 LunchDinneretc.

    }

    echo 
    $long_list;
    echo 
    $short_list;

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

    ?>

  • #2
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    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
    Code:
    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
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    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>";
            }
        }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •