Sort list by string

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

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Sort list by string

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, ill try that out now, thanks!
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :/
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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";
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 :wink:
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

wohoooo!
two answers with script included :D
thanks both Weirdan & DuFF!
It have already been christmas :P :wink:
Post Reply