Re: Sorting the output (opendir() and readdir())
| From: | DL Neil | Date: | Sat, 12 Jan 2002 12:16:00 +0000 |
| Subject: | Re: Sorting the output (opendir() and readdir()) | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-15695@lists.php.net to get a copy of this message | ||
Christian,
> we have a directory with pics, that a user can associate with his data in a
> form. we use opendir/readdir to show the directory contents. unfortunately,
> this is not sorted. How can we achieve a listing in alphabetical order?
>
> this is our code:
>
> echo "<select name='image'>";
> echo "<option value=''>please select</option>";
> $fp = opendir("../pics/");
> while ($file = readdir($fp)) {
> $selectfile="/pics/".$file;
> if ($file=="." OR $file=="..") { } else {
> $string_image .= "<option value='/pics/".$file."'";
> if ($selectfile==$image) { $string_image .= " SELECTED"; }
> $string_image .= ">".$file."</option>";
> }
> }
> echo $string_image;
> echo "</select>";
=readdir -- read entry from directory handle
string readdir (resource dir_handle)
Returns the filename of the next file from the directory. The
filenames are not returned in any particular order.
=so it is not possible to retrieve the filenames in alpha sequence.
=Instead of taking the filenames directly from the readdir() and dropping them into the
<option>, it may be
necessary to put them first into an array/list. At the end of the while loop, the array could be
sorted, and
then another (for) loop implemented to copy the sorted list from the sorted array/list into the
<option>s.
=Regards,
=dn