/    Sign up×
Community /Pin to ProfileBookmark

Creating from a while loop

Here is my problem, I am creating a body fat and muscle gain tracker that will need to perform simple math based on data previously entered. I’ll try my best to give an example without confusing whoever reads this post.

Someone would enter a weight of 214 lbs and 28.6% body-fat into my web form and then click on the save button, which would save and automatically redirect them to a table which shows their total results, which in this case would be 214 total pounds, 28.6% body-fat, 152.8 lean mass, and 61.2 pounds of fat which is automatically calculated. I know how to do all of this and this works out just fine. The problem arises when they would come back the following week and each week thereafter and enter their new results. For example:

Next week, after exercising, dieting and lifting weights their weight would be 212 pounds and 27.7% body-fat, so their new lean mass would be 153.8 pounds and their new total fat would be 58.7 pounds. This means they would have lost 2.5 pounds of fat and gained about .5 pound of muscle in the week. So basically the table would look something like this:

weight bf% t-fat t-lean fat loss muscle gain

214 | 28.6 | 161.2 | 152.8 | n/a | n/a
212 | 27.7 | 158.7 | 153.3 | -2.48 | 0.48
210 | 25.5 | 153.5 | 156.5 | -5.17 | 3.17
205 | 23.7 | 148.6 | 160.3 | -4.9 | -0.04

The problem is that my table is being generated using a while loop which so I don’t know how I would assign variables to “fat lost” and “muscle gained” because those last two values are simply adding or subtracting themselves from the info entered the previous week of this example; and with while loops the <table> and </table> tags are outside of the loop and the single <tr> row with all the <td></td> columns would be the single line generated as long as values are still available to be entered; so in the while loop I don’t know how to refer back to the previously entered values after the loop curser has moved down and entered the next table row or results.

I really hope I didn’t confuse everyone, it actually took me almost an hour to write this post. Help please?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@DerokorianDec 11.2012 — You need to track the previously fetched row. I'm looping through an array, but the method would be the same.

[code=php]
$arr = array(
array("a" => 100,
"b" => 90),
array("a" => 87,
"b" => 85),
array("a" => 70,
"b" => 77));
$prev = array();
echo "<table><tr><th>Current A</th><th>Current B</th><th>Diff A</th><th>Diff B</th></tr>";
while( $row = current($arr) ) {
echo '<tr><td>'. $row["a"] .'</td><td>'. $row["b"] .'</td><td>';
if( !isset($prev["a"]) ) {
echo "n/a";
} else {
echo $prev["a"] - $row["a"];
}
echo '</td><td>';
if( !isset($prev["b"]) ) {
echo "n/a";
} else {
echo $prev["b"] - $row["b"];
}
echo '</td></tr>';
$prev = $row;
next($arr);
}
echo "</table>";
[/code]
×

Success!

Help @jawaune spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...