Jump to content

Question about while loop


RIRedinPA

Recommended Posts

I'm trying to write a bit of code that will display between 1 and 3 random images, but I don't want to duplicate any of the displayed images. It would appear however that I have written an endless loop, though I keep looking at it and can't see why. Could someone take a look at the code below and see where I have erred or if I am even approaching this correctly, am I making it more complicated than I need to? Thanks.

 

<?php

srand ((double) microtime( )*1000000);
//get a random number of image from 1 to 3
$numofimages = rand(0,3);

//set up array and var
$imagelist = array(); 
$imagedisplay = "";

//loop through image count
for ($i=0; $i<$numofimages; $i++) {
		//set up the negative condition
	$canuse = false; 
	//loop while we are still in a negative condition
	while ($canuse == false) {
		//randomly choose image
		$thisimage = "img" . rand(0, 18);
		//loop through image array to see if we are already using it
		for ($a=0; $a<count($imagelist); $a++) {
		 	$imagetocheck = $imagelist[$a];
		 	if ($imagetocheck != $thisimage) {
		 		$canuse = true; 
				//if not, add the image to the array
				$imagelist[] = $thisimage;
				//modify image display
				$imagedisplay .= " <img src='images/people/$thisimage'>";
			 }
		}
	}//end while
}//end loop through image count

echo $imagedisplay; 
?>

Link to comment
https://forums.phpfreaks.com/topic/133350-question-about-while-loop/
Share on other sites

The FOR statement inside the WHILE LOOP just runs through the array $imagelist, if $imagelist contains the random image created $canuse remains false, if it doesn't I change $canuse to true.

 

Wouldn't it run like this then

 

WHILE $canuse == false loop (check $canuse value)

select random image

FOR loop through array comparing images stored to selected image, if no matches change $canuse  value to true, add image to array

end FOR loop

//-->at the end of this loop, if there was no match, wouldn't $canuse now be true, thus breaking the WHILE loop?

end WHILE loop

 

Not with how your logic is. If that is how you want it I would change the for loop to this:

 

         //loop through image array to see if we are already using it
         for ($a=0; $a<count($imagelist); $a++) {
             $imagetocheck = $imagelist[$a];
             if ($imagetocheck != $thisimage) {
               //if not, add the image to the array
               $imagelist[] = $thisimage;
               //modify image display
               $imagedisplay .= " <img src='images/people/$thisimage'>";
               break; // insert break to kick out of the for.
             }
         }
         $canuse = true; 

 

That will yield your desired result and not create an infinite loop.

Archived

This topic is now archived and is closed to further replies.

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