PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Fri Dec 26, 2003 5:19 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Fri Dec 26, 2003 5:32 am
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
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Fri Dec 26, 2003 6:27 am
ok, ill try that out now, thanks!
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Fri Dec 26, 2003 10:12 am
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 :/
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Fri Dec 26, 2003 10:57 am
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";
DuFF
Forum Contributor
Posts: 495 Joined: Tue Jun 24, 2003 7:49 pm
Location: USA
Post
by DuFF » Fri Dec 26, 2003 10:59 am
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
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Fri Dec 26, 2003 5:11 pm
wohoooo!
two answers with script included
thanks both Weirdan & DuFF!
It have already been christmas