soupi Posted October 3, 2013 Share Posted October 3, 2013 Hi i need to Echo out this statement, Starting weight is: 180 Starting body fat is: 15%After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53%After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04%After losing 3 pounds of fat Weight is now 177 body fat is now: 13.56%After losing 4 pounds of fat Weight is now 176 body fat is now: 13.07%After losing 5 pounds of fat Weight is now 175 body fat is now: 12.57%After losing 6 pounds of fat Weight is now 174 body fat is now: 12.07% So this is what I have, im not sure how to subtract 1 pound to output 179 and so forth, any help and advice would be appreciated. Thank you. <?php$start_weight = 180;$start_body_fat = 27;$start_body_fat_percent = $start_body_fat/$start_weight * 100;$pounds_lost = 15;$first_number= 1;echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";echo "<br>"; echo "After losing 1 pounds of fat weight is now: "$start_weight"- ".$first_number." Starting body fat is: ".$start_body_fat_percent."%";?> Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/ Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2013 Share Posted October 3, 2013 (edited) You'll want to use a for loop. The code for the loop will be like for($i = 1; $i <= $pounds_lost; $i++) { // work out new body weight, take $i from $body_weight // recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100 // output how many pounds lost ($i), new weight and body fat percentage } Edited October 3, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452445 Share on other sites More sharing options...
soupi Posted October 3, 2013 Author Share Posted October 3, 2013 so would it be like this? <?php$start_weight = 180;$start_body_fat = 27;$start_body_fat_percent = $start_body_fat/$start_weight * 100;$pounds_lost = 15;echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";for ($i =1; <= $pounds_lost; $i++);{echo "After losing 1 pounds of fat weight is now: $number1" ; echo " & Starting body fat is: ".$start_body_fat_percent1."%"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452455 Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2013 Share Posted October 3, 2013 (edited) Not quite, the loop needs to recalculate the weight and body fat on each iteration. for($i = 1; $i <= $pounds_lost; $i++) { //work out new body weight, take $i from $body_weight $new_weight = calculate new weight here; //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100 $new_body_fat_percent = calculate new body fat percent here; // output how many pounds lost ($i), new weight and body fat percentage echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%"; } On each iteration of the loop you're giving new values to $new_weight and $new_body_fat_percent. The output from the loop will then look like After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53%After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04%After losing 3 pounds of fat Weight is now 177 body fat is now: 13.56%After losing 4 pounds of fat Weight is now 176 body fat is now: 13.07%After losing 5 pounds of fat Weight is now 175 body fat is now: 12.57%After losing 6 pounds of fat Weight is now 174 body fat is now: 12.07% Edited October 3, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452459 Share on other sites More sharing options...
soupi Posted October 3, 2013 Author Share Posted October 3, 2013 $new_weight =$number1= $start_weight-1; ?? will that be the new weight? Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452460 Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2013 Share Posted October 3, 2013 Do you know what for loop do? have you read http://php.net/for yet? Learn how the for loop works then try again. I have told you what you need to do to calculate the new weight and body fat. Loook at the lines that start with // in the code I posted. Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452461 Share on other sites More sharing options...
soupi Posted October 3, 2013 Author Share Posted October 3, 2013 Do you know what for loop do? have you read http://php.net/for yet? Learn how the for loop works then try again. I have told you what you need to do to calculate the new weight and body fat. Loook at the lines that start with // in the code I posted. <?php $start_weight = 180; $start_body_fat = 27; $start_body_fat_percent = $start_body_fat/$start_weight * 100; $pounds_lost = 15; echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%"; for($i = 1; $i <= $pounds_lost; $i++) { //work out new body weight, take $i from $body_weight*/ $new_weight = $i-$pounds_lost //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100 $new_body_fat_percent = $i-$start_body_fat/$new_weight *100; // output how many pounds lost ($i), new weight and body fat percentage echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%"; } ?> I have a sytax on the red line, I kno what for loop does, but this is my first example. Im sorry Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452462 Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2013 Share Posted October 3, 2013 (edited) This line $new_weight = $i-$pounds_lost Is causing the error. It should have a semi-colon (the ; character) at the end of the line. Also $i-$pounds_lost should be $i-$start_weight Your on the right path now. Edited October 3, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452466 Share on other sites More sharing options...
soupi Posted October 3, 2013 Author Share Posted October 3, 2013 Im receiving a syntax error on this line echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%";, can you atleast show me how to print the first statement After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53%After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04% Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452474 Share on other sites More sharing options...
Solution Ch0cu3r Posted October 3, 2013 Solution Share Posted October 3, 2013 (edited) Fixed code. <?php $start_weight = 180; $start_body_fat = 27; $start_body_fat_percent = $start_body_fat/$start_weight * 100; $pounds_lost = 15; echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%<br />"; for($i = 1; $i <= $pounds_lost; $i++) { //work out new body weight, take $i from $body_weight*/ $new_weight = $start_weight - $i; //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100 $new_body_fat_percent = ($start_body_fat - $i)/$new_weight *100; // clean up $new_body_fat_percent; $new_body_fat_percent = number_format($new_body_fat_percent, 2, '.', ','); // output how many pounds lost ($i), new weight and body fat percentage echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%<br />"; } ?> Edited October 3, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452475 Share on other sites More sharing options...
soupi Posted October 3, 2013 Author Share Posted October 3, 2013 GOT IT <?php$start_weight = 180;$start_body_fat = 27;$start_body_fat_percent = $start_body_fat/$start_weight * 100;$pounds_lost = 15;echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%";for($i = 1; $i <= $pounds_lost; $i++){ //work out new body weight, take $i from $body_weight $new_weight = $start_weight - $i; //new body fat percentage, take $i from $start_body_fat and divide by new body weight $new_body_fat_percent = (($start_body_fat - $i) / $new_weight) * 100; // new weight and body fat percentage echo "<br/>After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%";}?> Quote Link to comment https://forums.phpfreaks.com/topic/282685-subtraction-question/#findComment-1452476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.