# Thread: Creating from a while loop

1. Registered User
Join Date
May 2003
Location
Florida
Posts
41

## 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. 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
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.11157 seconds
• Memory Usage 2,858KB
• Queries Executed 15 (?)
More Information
Template Usage (33):
• (1)SHOWTHREAD
• (1)ad_footer_end
• (1)ad_footer_start
• (1)ad_global_above_footer
• (1)ad_global_below_navbar
• (1)ad_global_header1
• (1)ad_global_header2
• (1)ad_navbar_below
• (1)ad_showthread_firstpost_sig
• (1)ad_showthread_firstpost_start
• (1)ad_thread_first_post_content
• (1)ad_thread_last_post_content
• (1)bbcode_php
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (1)header
• (1)headinclude
• (1)headinclude_bottom
• (2)memberaction_dropdown
• (1)navbar
• (4)navbar_link
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (2)postbit
• (2)postbit_onlinestatus
• (2)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
• showthread
Included Files (26):
• ./showthread.php
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/functions_navigation.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_facebook.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_threadinfo_query
• fetch_threadinfo
• fetch_foruminfo
• load_show_variables
• load_forum_show_variables
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• showthread_start
• showthread_getinfo
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• fetch_musername
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• showthread_post_start
• showthread_query_postids
• showthread_query
• bbcode_fetch_tags
• bbcode_create
• showthread_postbit_create
• postbit_factory
• postbit_display_start
• bbcode_parse_start
• postbit_imicons
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete
• build_navigation_data
• build_navigation_array
• check_navigation_permission
• process_navigation_links_start
• process_navigation_links_complete
• set_navigation_menu_element
• build_navigation_menudata
• build_navigation_listdata
• build_navigation_list
• set_navigation_tab_main
• set_navigation_tab_fallback
• navigation_tab_complete
• fb_like_button
• showthread_complete
• page_templates