fricx Posted January 11, 2010 Share Posted January 11, 2010 Hello, I am trying to list images from folder, and i have managed to that. But, I want to limit number of images the code will show. Here is my code: <?php $imgdir = 'uploads/image/'; $allowed_types = array('jpg','jpeg'); $a_img = glob($imgdir."*.{".implode(',',$allowed_types)."}",GLOB_BRACE); sort($a_img); echo '<table cellspacing="0" cellpadding="6"><tr>'; foreach ($a_img as $img) { if ($colCount % 6.5 == 0) echo "</tr><tr>"; $colCount++; $showimage = basename($img); echo '<td><div class="smallpic" style="width:100px;height:75px;"><a href="../index.php?pic='.$showimage.'"><img src="../uploads/image/'.$showimage.'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$showimage.'")" /></a></div</td>'; } echo "</tr></table>"; ?> So my question is how to limit above code to show only for example 4 images. Quote Link to comment https://forums.phpfreaks.com/topic/188030-listing-images/ Share on other sites More sharing options...
tail Posted January 11, 2010 Share Posted January 11, 2010 Perhaps you may want to use a for loop instead of foreach. for($i=0;$i<LIMIT;$i++) { //your code here } Quote Link to comment https://forums.phpfreaks.com/topic/188030-listing-images/#findComment-992702 Share on other sites More sharing options...
RichardRotterdam Posted January 11, 2010 Share Posted January 11, 2010 From the looks of your code it seems you're trying to create a multi column gallery of some sort. have you looked at the following? http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/188030-listing-images/#findComment-992719 Share on other sites More sharing options...
salathe Posted January 11, 2010 Share Posted January 11, 2010 Since glob returns an array, you can use array_slice to get a specific slice of it (e.g. $a_imgs = array_slice($a_img, 0, 10); to get the first 10 files). Quote Link to comment https://forums.phpfreaks.com/topic/188030-listing-images/#findComment-992729 Share on other sites More sharing options...
fricx Posted January 11, 2010 Author Share Posted January 11, 2010 Thank you salathe, array_slice() never came on my mind. The thin I did was that I used asort function to sort images in array in ascending order and then used array_slice() to define number of images to show. Thank you very much ! Quote Link to comment https://forums.phpfreaks.com/topic/188030-listing-images/#findComment-993199 Share on other sites More sharing options...
salathe Posted January 12, 2010 Share Posted January 12, 2010 You're welcome. I do tend to find that folks 'forget' about the range of array (and same for string) functions and just need reminding. If you don't mind, I've marked this thread as "solved" (though that's normally your responsibility) -- if you think it's not, please do click the button to mark it not solved. Quote Link to comment https://forums.phpfreaks.com/topic/188030-listing-images/#findComment-993414 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.