Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
May 24, 2012, 12:13:40 PM

Pages: [1]   Go Down
  Print  
Author Topic: sorting problem in PHP script  (Read 1498 times)
begeiste
Galactic Royalty
*****
Offline Offline

Posts: 371


WWW
« on: August 16, 2011, 11:48:46 AM »

Hi,

Can someone please help me out for the sorting problem for this php script? Because there is no an ability for sorting directories or files?

Any responses will be greatly appreciated it.

Code:
$path = "docs/";

function createDir($path = '.')
{
if ($handle = opendir($path))
{
echo "<ul>";

while (false !== ($file = readdir($handle)))
{
if (is_dir($path.$file) && $file != '.' && $file !='..')
printSubDir($file, $path, $queue);
else if ($file != '.' && $file !='..')
$queue[] = $file;
}

printQueue($queue, $path);
echo "</ul>";
}
}

function printQueue($queue, $path)
{
foreach ($queue as $file)
{
printFile($file, $path);
}
}

function printFile($file, $path)
{
echo "<li><a href=\"".$path.$file."\" target=\"_blank \">$file</a></li><br><br>";
}

function printSubDir($dir, $path)
{
echo "<li><span class=\"toggle\">$dir</span>";
createDir($path.$dir."/");
echo "</li>";
}

createDir($path);

Logged
smartkathy
Trekkie
**
Offline Offline

Posts: 17


« Reply #1 on: February 22, 2012, 04:08:31 AM »

I would love to know myself, the answer to this. Was looking for something like this.
Cheers.
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5215



« Reply #2 on: February 28, 2012, 02:58:44 PM »

I'm not sure what the original author is trying to do with that very poorly written code ("Create Dir" lists a directory? Really?), but if the intent is to sort the directory and file names, PHP has functions built in to sort strings. You just get the names into an array and feed it to sort(&$arrayName, SORT_STRING).
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
ericlewis107
Space Explorer
***
Offline Offline

Posts: 6


« Reply #3 on: April 12, 2012, 03:56:43 AM »

You can sort your files by putting them in array and use array sorting like asort(), rsort() etc.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: