...

View Full Version : Urgent Help with PHP/MySQL Calorie Counter Program



Adio DC
03-25-2010, 06:38 PM
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 :(

met
03-25-2010, 07:37 PM
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



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

Adio DC
03-25-2010, 09:57 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum