Jump to content

Infinite Loop... Sometimes


ajbarlow87

Recommended Posts

Hi guys. I've built an application for my company, and everyone likes it, but on some of the "events" they create, there is an infinite loop. It doesn't always happen, so i was investigate the 'else' part of it. I've looked over the script a few times over the past few months, not religiously until today. i still can't seem to find the issue. Can someone please help me?

 

<?php $rowtot=0; while($rowtot<count($resultsIOCC['IOCC'])){
 foreach ($resultsIOCC['IOCC'] as $IOCC ) {
 if( $IOCC->ioccCostID == $Cost->CostID ) {
 if( ($IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage) > 0 ) {
	 $rowtot = $IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage;
	 echo money_format('$%i',$rowtot) . '<br>';
	 $rowstot += $rowtot;
 } else {
	 echo "$" . number_format("0", 2) . '<br>';
	 $rowstot = ($rowstot + 0);
 }
 }
 $rowtot++;
 }
 }
?>

 

I can post more of it if necessary, but I believe it's in this part of the script. I know the script is not very "professional" looking and the variables are terribly named (like rowstot and rowtot), but it was a learning experience at the time, I didn't know PHP and it was my first project.

Link to comment
https://forums.phpfreaks.com/topic/273295-infinite-loop-sometimes/
Share on other sites

I don't think that while() loop is even needed. The while loop is supposed to continue as long as $rowtot is less than the number of records in the $resultsIOCC['IOCC']. But, inside the while loop there is a foreach() loop on the $resultsIOCC['IOCC'] array which increments $rowtot on each iteration. The net effect is that on the first iteration of the while loop the foreach loop will increase $rowtot such that there would never be a second iteration of the while loop. Perhaps you used the while loop to have the foreach() loop only run if the array contains records. If that's the case then you just need to do a simple if() condition check.

 

But, maybe that is not the case since I see you modify $rowtot in two places in the loop. One is a simple (plus one) at the end of the loop and then there is this line:

$rowtot = $IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage;

 

I suspect that might be the cause of your problem. If I am reading it all correctly, the while loop will continue as long as $rowtot < count($resultsIOCC['IOCC']). In the foreach loop you increment $rowtot by one on each iteration. So, you would think that the $rowtot would be at least equal to count($resultsIOCC['IOCC']) after the completion of the first iteration. But, you have that other line that can modify $rowtot. So, if that line above is decreasing $rowtot in some scenarios the loop could be infinite.

Ok, looking at the code in more detail I think I understand what you are trying to do and it is way, way more complicated than it should be. Take this for example:

		    if( ($IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage) > 0 )
	    {
		    $rowtot = $IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage;
		    echo money_format('$%i',$rowtot) . '<br>';
		    $rowstot += $rowtot;
	    }
	    else
	    {
		    echo "$" . number_format("0", 2) . '<br>';
		    $rowstot = ($rowstot + 0);
	    }

That basically says if the sum of those three values is > 0 then do something, else to the alternative. But, in the first condition you are taking the sum of those three values and outputting them in a money format. In the alternative you are basically doing the same thing, but with a hard coded value of 0. So, why the if() condition at all. If the sum is 0 just do the same thing. I believe this will do exactly the same thing - but without all the complexity and without the potential infinite loop.

 

$grand_total = 0;
foreach ($resultsIOCC['IOCC'] as $IOCC )
{
   if( $IOCC->ioccCostID == $Cost->CostID )
   {
    $sub_total = $IOCC->ioccLabor + $IOCC->ioccTravel + $IOCC->ioccMileage;
    echo money_format('$%i', $sub_total) . "<br>\n";
    $grand_total += $rowtot;
   }
}

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.