0

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++;


                            }
            }
3
  • 4
    Please, don't use 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. Commented Feb 25, 2013 at 16:46
  • $calctoday is 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 the time() function, and save your program a load of work. Or even better, use the DateTime class and the DateTime::diff() function; it's a lot more powerful than strtotime(). Commented Feb 25, 2013 at 16:46
  • This also seems like something that could easily be done via SQL query itself. Commented Feb 25, 2013 at 16:58

2 Answers 2

1

What happens in $sd = start_check_gap($row[1],45);? I.e. what is the value of $sd?

The first while loop will never exit, as nothing modifies the contens of $data.

while($data > 365)
{
    $pernum1 = $pernumc1;
    $pernumc1++;
}

The same applies for the second and third while loops.

while($data < 365)
{
    $pernum2 = $pernumc2;
    $pernumc2++;
}

// ...

while($data != FALSE)
{
    $pernum3 = $pernumc3;
    $pernumc3++;
}

Another oddity you may want to look at is:

$calctoday = date("Y-m-d");
// ...
$dateDiff = strtotime($calctoday) - strtotime($sd);

This could be replaced by:

$calcToday = time();
// ...
$dateDiff = strtotime($calctoday) - strtotime($sd);

Furthermore, as $calcToday is the same throughout the whole calculation, it can be moved outside the while loop construct.

Sign up to request clarification or add additional context in comments.

Comments

0

You have quite a few problems, but heres one case:

while($data < 365) {
    $pernum2 = $pernumc2;
    $pernumc2++;
}

If $data is ever < 365 the execution will enter this loop and never exit, because $data is not manipulated in any way. The same seems to happen inside every single one of your loops, except the first one, that happens to "fix itself" because mysql_fetch_array eventually returns false.

Another small issue:

$calctoday = date("Y-m-d");

You do this inside the loop that goes through the mysql results, this is totally unnecessary, especially considering your lowest unit is a day, move this outside the loop so it doesnt get recalculated on every mysql result line.

number_format($data, 0, '.', '');

PHP number_format converts a number into a human readable string, but after you convert it into a string you compare it to an integer (365), wouldn't it be better to keep it as a number and convert it later if absolutely necessary?

$pernumc1 = 0;
while($data > 365)
{
    $pernum1 = $pernumc1;
    $pernumc1++;
}

Inside the main loop, if $data > 365, you run this code. What is this code doing? its setting $pernumc1 to 0, then entering an endless loop (already addressed), then setting $pernum1 = $pernumc1 (basically, 0), and finally increasing $pernumc1 by 1, so it will never be more than one (or, in this case, it will be MAX_INTEGER since the loop doesnt end).

Read up on "control structures", find some tutorials, and organize in your mind exactly what you are going to do, then program.

1 Comment

Ok that makes sense, I'll redo the date conversions and try and explain the need for the calculations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.