Forum Moderators: coopster

Message Too Old, No Replies

How to sort results in my script

         

mr_nabo

9:55 pm on Jun 28, 2009 (gmt 0)

10+ Year Member



Hi,

Hopefully some kind person can help me. I need to sort the results in the part marked 'SORT RESULTS NUMERICALLY?' in my script below.

The files in the directory are named like so: 090628-name-of-file.php so I want to show the latest dated file at the top of the results.

Can anyone help me add this functionality please?

Thanks,

osu


<?php
// SHOW DIRECTORY CONTENTS IN BROWSER
// Start list structure
echo '<ul>';

// Define the full path to your folder from root
$path = "/path here";

// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");

// Loop through the files
while ($file = readdir($dir_handle)) {

// CLEAN FILENAME
// Strip file extension
$title = substr_replace($file,"",-4);

// Strip #*$!#*$!- date from beginning of filename
$title = substr($title,7);

// Replace '-' with spaces
$title = str_replace("-"," ",$title);

// Make it a teaser title
$title = substr($title, 0, 50);

// Finally, capitalise each word
$title = ucwords($title);

if($file == "." ¦¦ $file == ".." ¦¦ $file == "index.php" )

continue;

// SORT RESULTS NUMERICALLY?
echo '<li>';
echo "$title... <a href=\"$file\">read more</a><br />";
echo '</li>';
}

// Close list structure
echo '</ul>';

// Close directory
closedir($dir_handle);

?>

omoutop

8:12 am on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



something like this perhaps will help you or give you an idea:

$mydir = dir($path);
while(($file3 = $mydir->read()) !== false)
{
$ext3=explode('.',$file3); // modify this part if your fileames can contain more than one dot
if($file3 != '.' && $file3 != '..' && $ext3[1] && $file3!="Thumbs.db")
{
$array_thumb[]=$file3;
}
}

if ($array_thumb!="")
{
sort($array_thumb); // this arranges your array

foreach ($array_thumb as $img)
{
// do what you like with your single file
}
}

More info on sort() [us3.php.net]