user1221768
user1221768

Reputation: 45

php - while counter not working

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


                            }
            }

Upvotes: 0

Views: 161

Answers (2)

cernunnos
cernunnos

Reputation: 2806

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.

Upvotes: 0

nikc.org
nikc.org

Reputation: 16952

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.

Upvotes: 1

Related Questions