Jump to content

Infinite Loop... Sometimes


ajbarlow87
Go to solution Solved by Psycho,

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
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.

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.