Jump to content

for loop within...


jimfog
 Share

Recommended Posts

Is it valid to have a for loop within a for loop? I tried is and the browser loops endless, if i do not close the browser tab it will not stop looping. So, do you think that by definition is invalid or that it is sth else that causes the above problem?

Link to comment
Share on other sites

yes you can, there are no rules stating that you can't. However, when using incorrect logic, you can easily run into an infinite loop. It would have been appropriate to show a code snippet detailing your situation so we could provide more specific feedback.

Link to comment
Share on other sites

Ok then, despite the problem is sth more important-for which i trying to find a solution and for which the loop problem is only part of it-for now we will focus ONLY on the problem, and later, here, or in another post we will focus on the bigger picture. I am doing this so that we can focus better. Here is the code with for loops(one inside the other):

 for ($i = 0; $i < (($maxday-1) +$startday); $i++) {     // code....various if statements here	   elseif (isset($_GET['week'])&&((date("d",$previousmonday))==($i - $startday + 2)||((27)==($i - $startday + 2)))) {   for($i=0;$i<7;$i++){		   echo "<td class='weekgreen' align='center' valign='middle' height='20px'>" . ($i - $startday + 2) . "</td>\n";}	  		 }	  		  		  else {		    ?><td class='calendar-day' align='center' valign='middle' height='20px'><?php echo  ($i - $startday + 2) ?> </td><?php		 }

$maxday is 28-31(the day a month ends) and startday is 1. The above code produces an infinite loop.Why do you think?

Link to comment
Share on other sites

well, I can't really see where the if starts, but I can guarantee you are going to run into issues if you have nested loops both using the same iterator variable. i i.e. I believe you will always be reseting $i to zero in this case when you run the inner loop, which will force the outer loop to never increment past 0.

Link to comment
Share on other sites

well, I can't really see where the if starts,
Here is the if statement
if (isset($_GET['week'])&&((date("d",$previousmonday))==($i - $startday + 2)||((27)==($i - $startday + 2)))) {   for($i=0;$i<7;$i++){				   echo "<td class='weekgreen' align='center' valign='middle' height='20px'>" . ($i - $startday + 2) . "</td>\n";}		 				 }

but I can guarantee you are going to run into issues if you have nested loops both using the same iterator variable. i i.e. I believe you will always be reseting $i to zero in this case when you run the inner loop, which will force the outer loop to never increment past 0.
Are you implying yo use a different name variable for the inner loop, other than i, k for example?
Link to comment
Share on other sites

When the inner loop ends $i is always set to 7, and then the next time the outer loop runs it adds 1 to $i and starts the inner loop again, which puts $i back to 7, and over and over. You need to use different loop counters for each nested loop.

Link to comment
Share on other sites

You need to use different loop counters for each nested loop.
Which I did and the problem was solved, thanks man.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...