I am trying to create a counter based on results from an sql query. The sql query grabs a bunch of data, then row by row calculates a number of days duration, and then based on the total number of days it should count how many results fall within that day. Ive been trying to figure out why my counters are not retaining any values. I know the sql results are pulling data. Any ideas?
The idea is to see if the number of days between start date and current date is greater than 365 days then start a counter, and if its less than 365 days start a different counter.
$anymatches=mysql_num_rows($result_id);
if ($anymatches > 0 )
{
while($row = mysql_fetch_array($result_id))
{
/*** Performing a calculation to get the number of days ***/
$calctoday = date("Y-m-d"); // trying to capture current date
$sd = start_check_gap($row[1],45); // getting a date from the sql query
$dateDiff = strtotime($calctoday) - strtotime($sd); // probably a better way to do this but calculating the difference between start date and current date.
$totaldays = floor($dateDiff/(60*60*24));
$data = $dateDiff / 86400;
$data = number_format($data, 0, '.', '');
if ($data > 365)
{
$pernumc1 = 0;
while($data > 365)
{
$pernum1 = $pernumc1;
$pernumc1++;
}
}
elseif ($data < 365)
{
$pernumc2 = 0;
while($data < 365)
{
$pernum2 = $pernumc2;
$pernumc2++;
}
}
else
{
$pernumc3 = 0;
while($data != FALSE)
{
$pernum3 = $pernumc3;
$pernumc3++;
}
}
Thank you all for your comments below is what I got working. I wanted to post my correct version incase anyone else has the same type of issue. I was able to figure out what the issue was based on your infinite loop comments, well both issues. The first problem is my sql query had an error in it. Once I got the error corrected then I noticed the infinite looping issue you guys mentioned. Basically below is what I did. I removed the while's inside each if() and moved the beginning counter variable $pernumc1 to above the first while and it worked like a charm. Looks like i still need to clean up the date comparisons but overall it works.
$anymatches=mysql_num_rows($result_id);
if ($anymatches > 0 )
{
$pernumc1 = 0;
$pernumc2 = 0;
$pernumc3 = 0;
while($row = mysql_fetch_array($result_id))
{
$calctoday = date("Y-m-d");
$sd = start_check_gap($row[1],45);
$dateDiff = strtotime($calctoday) - strtotime($sd);
$totaldays = floor($dateDiff/(60*60*24));
$data = $dateDiff / 86400;
$data = number_format($data, 0, '.', '');
if ($data > 548)
{
$pernum1 = $pernumc1;
$pernumc1++;
}
elseif ($data > 365)
{
$pernum2 = $pernumc2;
$pernumc2++;
}
elseif ($data < 365)
{
$pernum3 = $pernumc3;
$pernumc3++;
}
}
mysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.$calctodayis completely unnecessary -- you're converting the current date into a formatted string, and then converting it straight back to a timestamp to compare. You could just use thetime()function, and save your program a load of work. Or even better, use theDateTimeclass and theDateTime::diff()function; it's a lot more powerful thanstrtotime().