Jump to content

Display the user's weight lost in the past week


Presto-X

Recommended Posts

Hello everyone,

 

I am working on a weight loss app for my site, and I want to display the user's weight lost in the past week, I have the current weight and the total weight lost today working great, but I was at a loss on how to get the weight for the past week to work  :-\

 

Here is the code that I'm working with:

mysql_select_db($database_getfit, $getfit);
$query_getfit_xml = "SELECT * FROM jos_my_weight ORDER BY `date` DESC"; // DESC and ASC
$getfit_xml = mysql_query($query_getfit_xml, $getfit) or die(mysql_error());
$row_getfit_xml = mysql_fetch_assoc($getfit_xml);
$totalRows_getfit_xml = mysql_num_rows($getfit_xml);

// START WEIGHT
$start_weight = '230';

// GOAL WEIGHT
$goal_weight = '175';

$rows = array();
while($row = mysql_fetch_assoc($getfit_xml)):
  $rows[] = $row;
endwhile;

$xml  = '<?xml version="1.0" encoding="utf-8"?>'."\n";
$xml .= '<chart>'."\n";
$xml .= '  <chart_data>'."\n";

// GET DATES
$xml .= '    <row>'."\n";
$xml .= '      <null/>'."\n";
foreach($rows as $row):
$xml .= '<string>'.$row['date'].'</string>'."\n";
endforeach;
$xml .= '    </row>'."\n";

// GET CURRENT WEIGHTS
$xml .= '    <row>'."\n";
$xml .= '      <string>Current Weight</string>'."\n";
foreach($rows as $row):
$xml .= '<number>'.$row['weight'].'</number>'."\n";
endforeach;
$xml .= '    </row>'."\n";

// GET TOTAL WEIGHT LOST TO DATE
$xml .= '    <row>'."\n";
$xml .= '      <string>Total weight lost to date</string>'."\n";
foreach($rows as $row):
$weight_lost = $start_weight - $row['weight'];
$xml .= '<number>'.$weight_lost.'</number>'."\n";
endforeach;
$xml .= '    </row>'."\n";

// GET WEIGHT LOST THIS WEEK
$xml .= '    <row>'."\n";
$xml .= '      <string>Total weight lost to date</string>'."\n";

$i = 0;
foreach($rows as $row):
$xml .= '<number>'.$row['weight'].'</number>'."\n";
$i++;
endforeach;

$xml .= '    </row>'."\n";

$xml .= '  </chart_data>'."\n";
$xml .= '</chart>';

echo $xml;

mysql_free_result($getfit_xml);

Right now the xml gets displayed like this:

 

<?xml version="1.0" encoding="utf-8"?>
<chart>
  <chart_data>
    <row>
      <null/>
      <string>2009-10-07</string>
      <string>2009-09-30</string>
      <string>2009-09-23</string>
      <string>2009-09-16</string>
      <string>2009-09-09</string>
      <string>2009-09-02</string>
      <string>2009-08-26</string>
      <string>2009-08-19</string>
      <string>2009-08-12</string>
      <string>2009-08-05</string>
    </row>
    <row>
      <string>Current Weight</string>
      <number>192</number>
      <number>197</number>
      <number>201</number>
      <number>205</number>
      <number>209</number>
      <number>212</number>
      <number>217</number>
      <number>221</number>
      <number>226</number>
      <number>230</number>
    </row>
    <row>
      <string>Total weight lost to date</string>
      <number>38</number>
      <number>33</number>
      <number>29</number>
      <number>25</number>
      <number>21</number>
      <number>18</number>
      <number>13</number>
      <number>9</number>
      <number>4</number>
      <number>0</number>
    </row>
    <row>
      <string>Total weight lost to date</string>
      <number>192</number>
      <number>197</number>
      <number>201</number>
      <number>205</number>
      <number>209</number>
      <number>212</number>
      <number>217</number>
      <number>221</number>
      <number>226</number>
      <number>230</number>
    </row>
  </chart_data>
</chart>

Are you trying to pull the data from the XML you created?  Why not pull it from MySQL and save yourself a hassle?  It would likely be something along the lines of:

SELECT SUM(weight) FROM jos_my_weight WHERE userid='$userid' AND `date` > DATESUB(NOW(), INTERVAL 1 WEEK)"

This assumes that you are tracking the entries with a userid.  Also, your column named "date" should be renamed if you wish to follow the best practices.

 

Otherwise, you will probably increment a value in these lines:

$weighttotal = 0;
foreach($rows as $row):
   $xml .= '<number>'.$row['weight'].'</number>'."\n";
   $weighttotal += $row['weight'];
$i++;
endforeach;

in order to get a total.  I'm not sure why you use "endforeach" instead of

foreach($rows as $row) {
    $xml ....
}//weight rows

Since I cannot edit -- the example I said regarding adding to $totalweight won't work as is.  You will have to determine if the date referring to your weight is within the range you want.  This is why I suggest doing it in MySQL.

Hello xtopolis,

 

Thanks for the reply, I will be the first to say my PHP skills are not the best, but I try to learn as much as I can every day.  The reason for me saving it to a xml file is I am calling this xml file with the XML/SWF Chart http://www.maani.us/xml_charts/.  Also as for my long endforeach I may not be doing this the best way, I like to know what I am closing, this comes in haddy later down the road.

 

I dated my table and my code I changed date to entered.

I think it should be easy as for the date as the user enters their weight once a week, so how would I go about grabbing the last date's weight then take current weight off of last week’s weight giving us total weight lost in the past week, but how do I get the last date each time foreach loops?

 

I was thinking grabbing the $last_date then save it to a session use $last_date to subtract current weight, then clear session use current weight and set $last_date's weight to the session again start loop over again.  Not sure if there is a better way I should be doing this.

ok I think I am close now, using the following code:

foreach($rows as $row):

    if(empty($weighttotal)):
      $current_weight = $row['weight'] - $row['weight'];
else:
  $current_weight = $row['weight'] - $weighttotal;
endif;
    $xml .= '<number>'.$current_weight.'</number>'."\n";
    $weighttotal = $row['weight'];

endforeach;

 

I get this outcome:

<row>
  <string>Total weight lost last week</string> 
  <number>0</number> 
  <number>5</number> 
  <number>4</number> 
  <number>4</number> 
  <number>4</number> 
  <number>3</number> 
  <number>5</number> 
  <number>4</number> 
  <number>5</number> 
  <number>4</number> 
</row>

 

So as you can see the first number is 0 becuase weighttotal is empty on the first pass, is there a way that I can reverse the order then do the math then reverse order one last time?  I do care if the last number is 0.

 

Ps. I did play around with sort($rows), sort($rows), asort($rows), arsort($rows), ksort($rows), and krsort($rows) with out any luck

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.