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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Urgent Help with PHP/MySQL Calorie Counter Program

    Hey guys,
    I'm an amateur coder and desperately need help with my Calorie Counter Program I'm trying to create. Basically here is my code:

    ****************
    [CODE]
    <?php
    $con = mysql_connect("portal", "6020", "hassank");
    //Connects to the SQL database under my username and log in.
    if (!$con)
    {
    die('Unable to Connect, contact Administrator: ' . mysql_error());
    }
    //If a problem occurs with connection an error message with guidance occurs for the users.
    mysql_select_db("hassank",$con);
    //Selects the database hassank
    $userlog = $_COOKIE["userlog"];
    $date = date("y-m-d") ;
    //Date

    $getuserID= "SELECT UserID FROM User WHERE UserName='".$userlog."'";
    //Selects the userID from the corresponding username in the cookies
    $result= mysql_query ($getuserID,$con);
    if (!$result)
    {
    die('Unable to Connect, contact Administrator: ' . mysql_error());
    }
    //Error checking for $getuserID, if there is a problem with the query an error message appears.
    $getuserIDresult= (INT)(mysql_result ($result,0,0));
    //Applies the result of the $getuserID query as integer from the result of the mysql $result query

    $consumedtotal= "SELECT FConsumed.Quantity, FConsumed.FDate, FConsumed.UserID, FConsumed.FoodID, Nutrition.Calories, Nutrition.Protein, Nutrition.Carbs,
    Nutrition.Fat, Nutrition.SatFat, Nutrition.Salt, Nutrition.Fibre FROM FConsumed INNER JOIN Nutrition ON FConsumed.FoodID = Nutrition.FoodID
    WHERE FConsumed.FDate = '".$date."' AND FConsumed.UserID = '".$getuserIDresult."'";
    //Query which joins together the two tables and selects the corresponding information for the consumed food
    $consumedtotalresult= mysql_query ($consumedtotal,$con);
    if (!$consumedtotalresult)
    {
    die('Unable to Connect, contact Administrator: ' . mysql_error());
    }
    //Error checking for $consumedtotal, if there is a problem with the query an error message appears.
    [CODE]
    ****************

    Basically I've innerjoined two tables in my database and now I'm stuck. I need to take all of the nutrition information which correspond to the food consumed on the current date, by the current user in these two tables and add them up. So basically as an example say the user had consumed 'two donuts today', I'd need the system to take the nutritional values of 'donut' multiply the values by the quantity and then display each respectively e.g. 'Calories, Protein etc' added up with whatever else has been consumed. As you can imagine multiple foods will be consumed each day and I need my system to be able to do this for as many foods as the user consumers.

    Can anyone help? I'm just starting out PHP and this is a project which really means alot to me. I'm not going to lie but I am COMPLETELY clueless

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    select the food and all associated data

    multiply each value by the amount consumed

    insert new values in to consumer table.

    query consumer table for all foods consumed on that day. loop through records

    PHP Code:
    $qry mysql_query"select all from table where date=today/requested date";) // won't work, psuedo

    while($r=mysql_fetch_array($qry)) {
        
    $protein=$protein $r['protein'];
        
    $fat $fat $r['fat'];
    }

    echo 
    'today you ate :' $protein
    if you've done the above yourself, what you are asking is somewhat easy.
    Last edited by met; 03-25-2010 at 07:41 PM.

  • Users who have thanked met for this post:

    Adio DC (03-25-2010)

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you so much, that was far simpler than anything I've previously tried!
    Is there a way that I can also multiply the quantity of each individual field while the count continues for example say I had 'two donuts and 1 slice of bread' because currently your method assumes their is only 1 of each consumed product. Many Thanks however!


  •  

    Posting Permissions

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