RIRedinPA Posted November 19, 2008 Share Posted November 19, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133350-question-about-while-loop/ Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 It seems like your for inside the while exists without ever setting canuse to true. Thus creating the endless loop, I would check your logic at that point and go on from there. Quote Link to comment https://forums.phpfreaks.com/topic/133350-question-about-while-loop/#findComment-693529 Share on other sites More sharing options...
RIRedinPA Posted November 19, 2008 Author Share Posted November 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133350-question-about-while-loop/#findComment-693540 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/133350-question-about-while-loop/#findComment-693546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.