# loop within a loop problem

## Recommended Posts

I have 2 loops, one is inside the other. What i am interested achieving is to terminate the outer loop earlier-instead going x iteration it must iterate x-something. I will demonstrate the code and the image(it is from the calendar i am building)-when you get to see the image you will understand exactly what i am talking:Here is the code:

`for (\$i = 0; \$i < ((\$maxday) + \$startday + \$sunday); \$i++) {				    //   for (\$i = 0,\$j=10;\$j<15, \$i < ((\$maxday) + \$startday + \$sunday); \$i++, \$j++) {	 					   					    if ((\$i % 7) == 0)                  					    echo "<tr class='juveline'>\n";				    if(\$i < \$sunday)					    echo "<td></td>\n";                 				    elseif((\$i - \$sunday + 1)==7)				    {echo '<td></td>';}				    elseif  ((date('d',\$previousmonday)) == (\$i - \$sunday + 1))				    {					    for (\$k = 1; \$k < 8; \$k++)					    {						 						    if(date("d")  == (\$i - \$sunday + \$k)) {//&&(date("d") == (\$i - \$startday + \$k))&&(date("n")==(date("n", strtotime(\$newdate)))))							    echo "<td class='nav-cur-highlited' align='center' valign='middle' height='20px'>" . (\$i - \$sunday + \$k ) . "</td>\n";							    }							    else							    echo "<td class='weekgreen' align='center' valign='middle' height='20px'>" .k.(\$i - \$sunday + \$k) . "</td>\n";													   						 }					 				    }				          					 else {					    echo "<td class='calendar-day' align='center' valign='middle' height='20px'>" . (\$i - \$sunday + 1) . "</td>\n";				    }				   				   				    if				    ((\$i % 7) == 6)					    echo "</tr>\n";			    }`

This is what is produce from the above code:https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!140&parid=root I want the numbers(days) in the red circle removed(they are part of the outer loop which needs adjustment), the numbers with the blue background is the week highlighted.

##### Share on other sites

You can use a break statement to get out of a loop. http://www.php.net/manual/en/control-structures.break.php

##### Share on other sites

At the point you want to exit from the inner and outer loop, add

`break 2;`

The break construct can take a number that specifies the number of loops to break from: In your case 2.

##### Share on other sites

break will just end the loop, I just want to modify the exerior one a little

##### Share on other sites

Well, there's not a built-in PHP statement to "modify the exterior loop a little", so maybe you just want to change the value of \$i or the other variables you're using to figure out when to stop the loop. You can also use a continue statement to stop processing the current iteration and go to the next.

##### Share on other sites

Finally did it, continue was the solution to the problem. I was not aware about the existence of this specific statement-i went to the php manual and from there it was straightforward. Thanks

Edited by jimfog