sac0o01 Posted July 12, 2010 Share Posted July 12, 2010 I am using the following to display all the images within a folder. How could I limit the number of images displayed to a set number, for example 10? $files = glob("temp/*.png"); for ($i=1; $i<count($files); $i++) { $num = $files[$i]; echo '<img src="'.$num.'" alt="random image">'." "; } Quote Link to comment https://forums.phpfreaks.com/topic/207502-help-displaying-images-in-a-folder/ Share on other sites More sharing options...
bh Posted July 12, 2010 Share Posted July 12, 2010 Hi, example break from the for loop like this: $num_of_images = 10; for ($i=1; $i<count($files); $i++) { $num = $files[$i]; echo '<img src="'.$num.'" alt="random image">'." "; if ($i >= $num_of_images) break; } or $num_of_imgs = count($files); $num_of_imgs = ($num_of_imgs > 11) ? 11 : $num_of_imgs; for ($i=1; $i<$num_of_imgs; $i++) { $num = $files[$i]; echo '<img src="'.$num.'" alt="random image">'." "; } Quote Link to comment https://forums.phpfreaks.com/topic/207502-help-displaying-images-in-a-folder/#findComment-1084844 Share on other sites More sharing options...
sac0o01 Posted July 12, 2010 Author Share Posted July 12, 2010 Thanks! Works great! I understand the first one but could you explain how this line in the second one works? Operators still give me a rough time. $num_of_imgs = ($num_of_imgs > 11) ? 11 : $num_of_imgs; Quote Link to comment https://forums.phpfreaks.com/topic/207502-help-displaying-images-in-a-folder/#findComment-1084858 Share on other sites More sharing options...
bh Posted July 12, 2010 Share Posted July 12, 2010 Youre welcome. Thats the ternary operator: (condition statement) ? 'if condition is true this will be the actual value' : 'if condition is false than this' Quote Link to comment https://forums.phpfreaks.com/topic/207502-help-displaying-images-in-a-folder/#findComment-1084864 Share on other sites More sharing options...
sac0o01 Posted July 12, 2010 Author Share Posted July 12, 2010 Thanks so much, looks like I have some more studying to do. Quote Link to comment https://forums.phpfreaks.com/topic/207502-help-displaying-images-in-a-folder/#findComment-1084871 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.