Page 1 of 1
URL alphabetization
Posted: Thu Sep 07, 2006 12:49 pm
by Daryn
Hi there.
I'm somewhat new to PHP and I have something that seems pretty simple to do. I just can't find something to do what I want.
I have a page where my client can login to a backend file uploader/indexer and upload the files he wants. After they are uploaded, the contents of the uploads folder are indexed and shown on this page
http://blessedtrinity.org/ministries/music.php at the bottom as a list of downloadable files.
The issue he has is that he wants the list of links to be alphabetized. Everything I've read suggests using an SQL database but I'm trying to find something that is simplified. I feel that having to create a db would be a little bit of a stretch for something so simple. I also read about using an array structure, but I'm not sure of how to write it.
Any help with this is appreciated.
Thank you!!!
Posted: Thu Sep 07, 2006 1:50 pm
by andym01480
Using a mysql database would be simpler than an array!
I think you would need to write to a text file each time a file is uploaded or deleted. Taking all the line of the file into an array processing it and putting it back. Alphabetising is easy using the sort functions. The file would need to be writable.
A database approach just means storing the filenames with a unique id - doing a simple query that sorts as it gets the results. A lot easier to code! Especially when it comes to deleting and updating.
Posted: Thu Sep 07, 2006 1:55 pm
by Mordred
Daryn, what is the PHP code that outputs those links?
Posted: Thu Sep 07, 2006 2:06 pm
by Daryn
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="Mordred"]Daryn, what is the PHP code that outputs those links?[/quote]
this is what i'm currently using.
Code: Select all
<?
//define the path as relative
$path = "music/files/";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo "<li><a href='music/files/$file'>$file</a></li>";
}
//closing the directory
closedir($dir_handle);
?>
it's within an unordered list so that the links output as bullet points.
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Sep 07, 2006 2:12 pm
by andym01480
Mmmm don't need a text file!:roll:
Use the while loop to put the file names in an array. Sort() the array then use another loop to output them.
Posted: Thu Sep 07, 2006 2:30 pm
by Daryn
andym01480 wrote:
Mmmm don't need a text file!:roll:
Use the while loop to put the file names in an array. Sort() the array then use another loop to output them.
that sounds like what i want to do. i didn't actually write what i used to index that directory though. i used it from a tutorial on a website and altered it to fit my directories (im more of a frontend guy

)
if you can be more specific, that would be awesome. im pretty green to php.
Posted: Thu Sep 07, 2006 3:11 pm
by andym01480
Code: Select all
<?php
//define the path as relative
$path = "music/files/";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
$filenames=array();
//running the while loop
while ($file = readdir($dir_handle)){
if($file!="." && $file!="..") {
$filenames[]=$file; //add the filename to array
}
}
//closing the directory
closedir($dir_handle);
sort($filenames); //sort filename array
//echo the array in alphabetical order!
foreach ($filenames as $key => $val) {
echo "<li><a href=\"music/files/$val\">$val</a></li>";
}
?>
worked on my server - although it sorts uppercase filenames into alphabetical and then lowercase - anyone any idea why???
Posted: Thu Sep 07, 2006 3:28 pm
by Daryn
andym01480 wrote:Code: Select all
<?php
//define the path as relative
$path = "music/files/";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
$filenames=array();
//running the while loop
while ($file = readdir($dir_handle)){
if($file!="." && $file!="..") {
$filenames[]=$file; //add the filename to array
}
}
//closing the directory
closedir($dir_handle);
sort($filenames); //sort filename array
//echo the array in alphabetical order!
foreach ($filenames as $key => $val) {
echo "<li><a href="music/files/$val">$val</a></li>";
}
?>
worked on my server - although it sorts uppercase filenames into alphabetical and then lowercase - anyone any idea why???
that's what i'm looking for! now when i look at what you've done, it makes sense to me.
all of the files that my client uploads are always capitalized so it is not an issue. this sorts it exactly the way i want it. thank you! ill study this and use it in the future.
Posted: Thu Sep 07, 2006 3:31 pm
by feyd
natcasesort() may be of interest.
The reason why sort places upper case first is because in binary the upper case letters are lower in ordinal value than the lower case letters.
Posted: Thu Sep 07, 2006 3:44 pm
by andym01480
Cheers feyd, thought you'd come up trumps.
And thanks Daryn for the chance to learn about opendir and readdir!
