Member Avatar for by89

userfood table

auto_id | userId | date | foodname | perserving | satfat | monofat | polyfat | protein | carb
1 ------ 1234567S --2010/02/01 -- chicken rice- --- 382g ----- 8.7 ----- 9.1 ---- 3.7 ------ 25 ----- 75
2 ------ 1234567S --2010/02/02 -- mee siam----- --- 683g ----- 8.6 ----- 9.1 ---- 4.9 ------ 28 ----- 92

calorieprofile table

auto_id | userId | date | protein | carb | fat
1 ------ 1234567S --2010/02/01 ---- 25 ---- 75 --- 42
2 ------ 1234567S --2010/02/02 ---- 28 ---- 92 --- 37

display out in this form

date | foodname | fat consume | daily fat | protein consmue | daily protein | carb consume | daily carb
2010/02/01 -- chicken rice -- --- 21.5 ---------- 42 ---------- 25 -------------- 25 ----------- 75 ---------- 75 ----
2010/02/01 ----- mee siam --- --- 22.6 ---------- 37 ---------- 28 -------------- 28 ----------- 92 ---------- 92 ----


I'm trying to sum the value satfat, monofat and polyfat from userfood table together and output as what it show in the display as the coulmns fat consume. But this 3 data is DOUBLE data type and i do not know how to go about adding and displaying. I also planning to join the userfood and calorie profile together to compare the fat, protein and carb that the user has consumed. But the problem is i'm not sure how to go about writing the sql statement. anyone can help,thanks.

The rules of database normalization often do call for separate tables, but only when based upon rules that make sense for that particular data. I'm no nutritionist but I am a database person, and I see no reason why you'd want these in separate tables. In fact, what I am seeing here actually goes against the rules of database normalization. Therefore, rather than using Join statements to run your queries, I would combine the tables permanently.

Until you do that, why not avoid additional complexity and work with the values in separate data arrays that your results will give you now, with simple SQL queries?

As for your math issue - a double type uses the same math functions as you'd use to add/subtract/multiply/etc. on integers or any other numeric data type in PHP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.