Page 1 of 1
Sort list by string
Posted: Fri Dec 26, 2003 5:19 am
by vigge89
How can i change this script to make it sort the results by name, and also, take away the ".css" extension?
Code: Select all
<?php
echo "<select name='skin'>";
$path = "skins/"
$dir_handle = @opendir($path) or die("Can't open directory '$path'.");
while (false !== ($file = readdir($dir_handle))) {
if ($file != "." && $file != "..") {
$file = $file
echo "
<option value='$file'>$file";
}
echo "</select>";
?>
But how co
Posted: Fri Dec 26, 2003 5:32 am
by volka
while (false !== ($file = readdir($dir_handle))) {
if ($file != "." && $file != "..") {
$file = $file
echo "
<option value='$file'>$file";
}
instead of echo'ing the filenames immediately store them in an array. You can get rid of the extension before doing so with
http://php.net/substr
Then sort the array with
http://php.net/sort and display each name
Posted: Fri Dec 26, 2003 6:27 am
by vigge89
ok, ill try that out now, thanks!
Posted: Fri Dec 26, 2003 10:12 am
by vigge89
BTW, how can i store all the files in an array, ie, how would i add the new $file into the array? i've never done that before :/
Posted: Fri Dec 26, 2003 10:57 am
by Weirdan
something like this:
Code: Select all
$path = "skins/"
$dir_handle = @opendir($path) or die("Can't open directory '$path'.");
$files=array();
while (false !== ($file = readdir($dir_handle)))
if ($file != "." && $file != "..")
$files[] = $file;
// now $files contain all the filenames, so:
foreach ($files as $file)
echo "here the file: $file<br/>\n";
Posted: Fri Dec 26, 2003 10:59 am
by DuFF
Just checked out the user comments for
readdir() and there was an example of what you are trying to do. Here's a simplified version:
Code: Select all
<?php
$path = "skins/";
$dir_handle = @opendir($path) or die("Can't open directory '$path'.");
while (false !== ($file = readdir($dir_handle))) {
if ($file != "." && $file != "..") {
$filelist[] = $file;
}
}
closedir($dir_handle);
echo "<select name='skin'>";
asort($filelist);
foreach ($filelist as $key => $val) {
echo "<option value="$val">$val</option>";
}
echo "</select>";
?>
Edit: Damn, Wierdan beat me to it

Posted: Fri Dec 26, 2003 5:11 pm
by vigge89
wohoooo!
two answers with script included

thanks both Weirdan & DuFF!
It have already been christmas
