www.webdeveloper.com
Results 1 to 2 of 2

Thread: Creating from a while loop

Hybrid View

  1. #1
    Join Date
    May 2003
    Location
    Florida
    Posts
    38

    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?

  2. #2
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    You need to track the previously fetched row. I'm looping through an array, but the method would be the same.

    PHP Code:
    $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>"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles