Reputation: 365
How can I break while loop and for loop too when while loop is true?
<?php
for($i=0; $i<4; $i++){
$result = mysql_query("SELECT * FROM table WHERE weight='$i'");
while($row = mysql_fetch_array($result)){
echo $row['cell'];
break; //I need to break the while loop and the for loop too in this line exactly, when he find and echo the "cell"!
}
}
?>
Upvotes: 2
Views: 5622
Reputation: 3160
Instead of doing a break
why not do the following
SELECT * FROM table WHERE weight BETWEEN 0 AND 3
OR just
SELECT * FROM table WHERE weight = 0
You would be limiting querying the database multiple times and probably takes less time but you'd wouldn't notice it with a query like this.
Not sure why you there is a for loop then break on first iteration when you can just query the database and echo something. Unless OP has other plans for this code and reduced the code for SO the loop is really unnecessary.
Upvotes: 1
Reputation: 5309
use exit instead of break.
break results exit from current iteration while exit results exit from overall pgm.
Upvotes: 0
Reputation: 1086
Use Flags:
One flag inside while : Set this flag to 1 when you break while
One flag in outer for loop: If while loop flag is set then break for loop
Upvotes: 1