Jump to content

How to stop a loop, and not the page.


Cronthenoob

Recommended Posts

I've got a loop, that I need to stop after a certain number of times through.If I use exit() then everything beyond that point doesnt load into the page. Is there another way I can stop the loop but load the rest of the page?Heres my code.

<?PHP	$query="SELECT * from cddb_albums order by album_id DESC";	$results=mysql_query($query);	echo mysql_error();		$newalbum=1;		while($e = mysql_fetch_array($results)) {		if ($newalbum <= 5) {			echo $e[album_title]."<br />";			$newalbum=$newalbum+1;			if ($newalbum == "6") {				exit();			}		}	}		?>

Link to comment
Share on other sites

I'm not entirely sure, but I think you can use break; to exit from the loop, but continue execution. If that doesn't work, you can recode your loop like so:

$continue = true;while($e = mysql_fetch_array($results) && $continue){  if ($newalbum <= 5)  {	echo $e[album_title]."<br />";	$newalbum=$newalbum+1;	if ($newalbum == "6")	  $continue = false;  }}

That will have the same effect as using break; in place of the $continue=false; line.

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