Jump to content

Help on Sorting


trevmart

Recommended Posts

Hi,

 

I have a piece of code that reads the directory, generates links to another page named after the file name with the link to the actual file and a text description. That part is pretty straight forward from below. I also take out the _ and the .pdf or .doc extensions from the file name. The files are Named File_of_any_length_January_2007.pdf. I did some sub stringing to take out the date in a mm_yyyy form. I want to sort the file names based on this attribute and am having problems figuring out how to do it. I am pretty new to PHP so it may just be something obvious.

 

Any help would be much appreciated to get this thing finished.

 

Thanks

<? 
$path = "/free_library";
// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");{
$filecount = "0"; //reset the number of files

while (false !== ($file = readdir($dir_handle))) {
if(preg_match("/\.pdf$|\.doc$/i", $file))
{
$fileList[] = trim($file); // add the file to an array so it can be sorted easily.
reset ($fileList); // go back to the top of the array
$filecount = $filecount + 1; //count the number of files
}
}

echo ' Number of Files in the Free Library: <b>'.$filecount.'</b><br><br>';
while (list ($key, $file) = each ($fileList)) {
$month = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$monthnumber = array("01","02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
$monthconvert = str_replace($month, $monthnumber, $file);
$monthclean = str_replace(".pdf", "", $monthconvert);
$length = strlen($monthclean);
$datepart = substr($monthclean,$length-7,$length);
$monthfinal = substr($datepart,0,2);
$strpubyr = substr($datepart,3,strlen($datepart));
$monthword = str_replace($monthnumber, $month, $monthfinal);
$strpubdate = $monthword . ', ' .$strpubyr;

$title = str_replace("_", " ", $monthclean);
$title = explode(".", $title);
$title = $title[0];
$title = str_replace($datepart,"", $monthclean);
$titlefinal = str_replace("_", " ", $title);

$name = $file;
$name = explode(".", $name);
$name = $name[0];


echo "<a href='research.php?varname=$name' class=black12>$titlefinal</a><br>$strpubdate<br/><br/>";

$ourFileName = "/free_library/des/$name.php";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle); 
$textcontent = file_get_contents("/free_library/des/$name.txt");
$myFile = "/free_library/des/$name.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<span class=blackb16>Free Research Library</span><br><br>";
fwrite($fh, $stringData);
$stringData = "<span class=blackb14>$titlefinal<br>$strpubdate</span><br><br>";
fwrite($fh, $stringData);
fwrite($fh, $textcontent);
$stringData = "<br><br><a href='/free_library/$file' target=\"new\" class=black12>Click to View</a><br><br>";
fwrite($fh, $stringData);
$stringData = "<a href='../research.php?varname=free_library' class=black12>Return to Free Library Contents</a>";
fwrite($fh, $stringData);
fclose($fh);
}


closedir($handle);
}


?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.