ppgpilot Posted April 19, 2010 Share Posted April 19, 2010 I've been coding PHP for 5 years and have not run into this problem. After 3 days it is time to ask for help. My script looks at two numbers and returns a % of the two. The first number is total calories and the second is protein calories. The object is to find the % of protein calories compared to total calories. (same with Fat and Carbs also) Protein calories / total calories should return a fraction which is the protein % of the Total Calories (i.e. 200 protein calories / 800 total calories = 25%) This works using $variables as long as the total calories is less than 1,000. Above 1,000, it does not work, but returns a number that doesn’t make sense. HOWEVER, it DOES work using the actual numbers and not $variables even though the $variables test good and contain the right amount. $protein_count1 = ($protein*4) / $total_caloriee; $protein_count2 = $protein_count1 * 100; $protein_count = number_format($protein_count2); Result for under 1,000 calories is correct Total Running Calories 428.00 Fat = 11 % Protein = 29 % Carbs = 54 % Result Over 1000 calories is incorrect Total Running Calories 2,881.31 Fat = 48,077 % Protein = 43,332 % Carbs = 53,364 % Correct numbers should be Fat = 33 % Protein = 30 % Carbs = 37 % These are the values that display when using the actual number and not feeding the number through a variable. ///////// ALSO!!!! Trying to filter the total calories with an IF statement isn’t working either !!! IF($total_calories >= 1000) even if the number is above 1,000, it returns false. if($total_calories > 1000) { echo "greater than $total_calories <br /><br />"; }else{ echo "less than $total_calories <br /><br />"; } Result is always “less than” whether < or > 1,000 - while showing the appropriate number of total calories . I have tried changing the variable names with the same results. This is very simple stuff and should work, but it isn’t. Any idea what is happening. Thanks for any help... David Link to comment https://forums.phpfreaks.com/topic/199038-bamboozled/ Share on other sites More sharing options...
andrewgauger Posted April 19, 2010 Share Posted April 19, 2010 The problem is you need to return the number_format back to an int before working on it. throw this in your function for both arguments. $arg1=str_replace (",","", $arg1); Link to comment https://forums.phpfreaks.com/topic/199038-bamboozled/#findComment-1044742 Share on other sites More sharing options...
litebearer Posted April 19, 2010 Share Posted April 19, 2010 Curious. Result for under 1,000 calories is correct Total Running Calories 428.00 Fat = 11 % Protein = 29 % Carbs = 54 % Result Over 1000 calories is incorrect Total Running Calories 2,881.31 Fat = 48,077 % Protein = 43,332 % Carbs = 53,364 % Correct numbers should be Fat = 33 % Protein = 30 % Carbs = 37 % These are the values that display when using the actual number and not feeding the number through a variable. in one example (the final one) the percentages total 100; while in the first they only total 94. I presume fat + protein + carbs = 100% ; or is there an additional factor? Link to comment https://forums.phpfreaks.com/topic/199038-bamboozled/#findComment-1044747 Share on other sites More sharing options...
litebearer Posted April 19, 2010 Share Posted April 19, 2010 Unless I am missing something... $protein = 650; $fat = 200; $carb = 345; $total = $protein + $fat + $carb; $pro_p = ($protein/$total)*100; $fat_p = ($fat/$total)*100; $car_p = ($carb/$total)*100; ?> <table border="1"> <tr><td>Protein</td><td><? echo $protein; ?></td><td><?PHP echo number_format ($pro_p, 2); ?> %</td></tr> <tr><td>Fat</td><td><? echo $fat; ?></td><td><?PHP echo number_format ($fat_p, 2); ?> %</td></tr> <tr><td>Carb</td><td><? echo $carb; ?></td><td><?PHP echo number_format ($car_p, 2); ?> %</td></tr> </table> Makes sense? (in action here http://nstoia.com/calorie.php) Link to comment https://forums.phpfreaks.com/topic/199038-bamboozled/#findComment-1044763 Share on other sites More sharing options...
ppgpilot Posted April 19, 2010 Author Share Posted April 19, 2010 Thank you andrewgauger - that solved the problem and I am up and running! I knew it was something simple. I need to ask for help sooner! Thank you litebearer - you are right, there are other products in the formula that together add up to 100%, but only tracking the fat, carb, and protein in this program. David Link to comment https://forums.phpfreaks.com/topic/199038-bamboozled/#findComment-1044799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.