SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array range with scandir

    Hello,

    I want to display pictures from a specific folder, but only some of them, the order doesn't matter as long as they are not repeated.

    This works fine, except it shows everything.

    Code:
     <?php $dirs = "azure/arts"; 
    
    $pics = scandir($dirs);
    
    foreach ($pics as $item) { if (($item != ".") && ($item != "..")) {
    
    echo '<a target="_blank" href="' . $dirs . $item . '"><img src="' . $dirs . $item . '" alt="' . $item . '" title="' . $item . '" class="drawgala" /></a> '; } } ?>
    I tried this, but it didn't work

    Code:
     <?php $dirs = "azure/arts"; 
    
    $pics = scandir($dirs);
    
    foreach ($pics(1, 50) as $item) { if (($item != ".") && ($item != "..")) {
    
    echo '<a target="_blank" href="' . $dirs . $item . '"><img src="' . $dirs . $item . '" alt="' . $item . '" title="' . $item . '" class="drawgala" /></a> '; } } ?>
    thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you going to determine what images are to be shown??

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    I have a feeling the answer to your question is going to be "use glob() instead", but like iamjones, i'm curious how you're actually filtering.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    It looks like you are wanting to use something like array_slice() to get a limited number of files from the full array.

    Code php:
    $chosen_pics = array_slice($pics, 0, 50);
    foreach ($chosen_pics as $item) {

    Bear in mind that this will include directories (even the . and .. ones), so it's also a good idea to filter the array. One way to get only the files from the array would be to use array_filter() with is_file().

    Code php:
    $pics = array_filter('is_file', $pics);
    $chosen_pics = array_slice($pics, 0, 50);
    foreach ($chosen_pics as $item) {
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you Salathe! that was exactly what I was looking for. ^_^


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •