order alfabetical many images

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Davide

    order alfabetical many images

    Hi all.

    With this simple script I can see all my jpg in a web page..
    but they are confused randomly in the page.

    is there a way to put them alfabetically? from A to Z?
    thanx

    <?php
    if ($handle = opendir('/cover')) {
    while (false !== ($file = readdir($handle ))) {
    if (($file != "." && $file != "..")) {
    echo " <img src=\"cover/$file\" alt=\"$file\" border=0> ";
    }
    }
    closedir($handl e);
    }

    ?>

  • jamen

    #2
    Re: order alfabetical many images

    Davide wrote:[color=blue]
    > Hi all.
    >
    > With this simple script I can see all my jpg in a web page..
    > but they are confused randomly in the page.[/color]
    [color=blue]
    > <?php
    > if ($handle = opendir('/cover')) {
    > while (false !== ($file = readdir($handle ))) {
    > if (($file != "." && $file != "..")) {
    > echo " <img src=\"cover/$file\" alt=\"$file\" border=0> ";
    > }
    > }
    > closedir($handl e);
    > }
    >
    > ?>
    >[/color]

    Put all the files in an array and sort it, then print

    $files = array();

    if (($file != "." && $file != "..")) {
    $files[] = $file
    }


    all done:

    asort($files);

    for each($files as $file){
    echo " <img src=\"cover/$file\" alt=\"$file\" border=0> ";
    }

    Comment

    • Ewoud Dronkert

      #3
      Re: order alfabetical many images

      jamen wrote:[color=blue]
      > Put all the files in an array and sort it [...]
      > asort($files);[/color]

      No need for asort(), just use sort(). See http://php.net/sort

      --
      E. Dronkert

      Comment

      • Davide

        #4
        Re: order alfabetical many images

        I correct the script with yours suggests but seems doesn't work :(

        <?php
        if ($handle = opendir('./cover')) {
        while (false !== ($file = readdir($handle ))) {
        $files = array($file);
        if (($file != "." && $file != "..")) {
        sort($files);
        foreach($files as $file){
        echo "<img src=\"cover/$file\" alt=\"$file\" border=0>";
        }
        }
        }
        closedir($handl e);
        }

        ?>

        Comment

        • jamen

          #5
          Re: order alfabetical many images

          Davide wrote:[color=blue]
          > I correct the script with yours suggests but seems doesn't work :(
          >
          > <?php
          > if ($handle = opendir('./cover')) {
          > while (false !== ($file = readdir($handle ))) {
          > $files = array($file);
          > if (($file != "." && $file != "..")) {
          > sort($files);
          > foreach($files as $file){
          > echo "<img src=\"cover/$file\" alt=\"$file\" border=0>";
          > }
          > }
          > }
          > closedir($handl e);
          > }
          >
          > ?>
          >[/color]

          if ($handle = opendir('./cover')) {
          $files = array();

          while (false !== ($file = readdir($handle ))) {
          if (($file != "." && $file != "..")) {
          $files[] = $file;
          }
          }
          closedir($handl e);
          sort($files);

          foreach($files as $file){
          echo "<img src=\"cover/$file\" alt=\"$file\" border=0>";
          }

          }

          Comment

          • Davide

            #6
            Re: order alfabetical many images

            it works!

            Thanx, really :)

            Comment

            Working...