1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Simple PHP script problem...

Discussion in 'PHP' started by skyblue1950, Feb 11, 2014.

  1. #1
    I've found this simple PHP script that takes images from a folder and list them in a page, the problem is that the images are listed alphabetically I think (which is fine, I use numbers at the end of the filename) but I want to to reverse the order, so instead of displaying filename1.jpg, filename2.jpg and so on it displays filename3.jpg, filename2.jpg, etc.
    <?php
    
    $folder = 'img/';
    $filetype = '*.*';
    $files = glob($folder.$filetype);
    echo '<table>';
    for ($i=0; $i<count($files); $i++) {
        echo '<tr><td>';
        echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
        echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
        echo '</td></tr>';
    }
    echo '</table>';
    ?>
    PHP:
    My knowledge is very limited to say the least but sounds very easy to do, if someone can help me out it would be awesome. Thanks
    SEMrush
     
    Solved! View solution.
    skyblue1950, Feb 11, 2014 IP
    SEMrush
  2. #2
    You have to reverse the for loop to start at the end of your array (count($files) - 1) rather than the beginning (0).

    <?php
    $folder = 'img/';
    $filetype = '*.*';
    $files = glob($folder.$filetype);
    echo '<table>';
    for ($i = (count($files) - 1); $i >= 0; $i--) {
        echo '<tr><td>';
        echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
        echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
        echo '</td></tr>';
    }
    echo '</table>';
    ?>
    PHP:
     
    Iconiplex, Feb 11, 2014 IP
    aidanriley629 and sarahk like this.
  3. skyblue1950

    skyblue1950 Member

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    That worked perfectly! Thank you Iconiplex for the quick solution, it was much easier than I thought.
     
    skyblue1950, Feb 11, 2014 IP
  4. Nand Lal

    Nand Lal Greenhorn

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #4
    awesome and simple solution. Great :)
     
    Nand Lal, Feb 21, 2014 IP
    aidanriley629 likes this.
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,178
    Likes Received:
    1,728
    Best Answers:
    239
    Trophy Points:
    515
    #5
    I'd just use an array sort... Though I'm wondering why if you only have one TD per TR why the blue blazes you're using a table... unless you're planning on adding information like filesize to it... much less why you'd output a numbered index that would invalidate every time the directory contents changed... or putting name attributes on the anchors like it's 1997...

    $files = arsort(glob($folder . $filetype));

    poof, reverse order.

    Also, you might want to look at:
    http://us2.php.net/pathinfo

    Since this:
    substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));

    could be easier done as this:
    pathInfo($fileName, PATHINFO_BASENAME)

    90% or more of the time you do brute-force string processing, particularly on things like filenames, the more likely it is there's already a function for that in PHP.
     
    deathshadow, Feb 21, 2014 IP