URL alphabetization

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
Daryn
Forum Newbie
Posts: 4
Joined: Thu Sep 07, 2006 12:39 pm

URL alphabetization

Post 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!!!
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Daryn, what is the PHP code that outputs those links?
Daryn
Forum Newbie
Posts: 4
Joined: Thu Sep 07, 2006 12:39 pm

Post by Daryn »

feyd | Please use

Code: Select all

,

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

,

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]
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

:oops:
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.
Daryn
Forum Newbie
Posts: 4
Joined: Thu Sep 07, 2006 12:39 pm

Post by Daryn »

andym01480 wrote::oops:
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 :oops: )

if you can be more specific, that would be awesome. im pretty green to php.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post 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???
Daryn
Forum Newbie
Posts: 4
Joined: Thu Sep 07, 2006 12:39 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

Cheers feyd, thought you'd come up trumps.

And thanks Daryn for the chance to learn about opendir and readdir! :lol:
Post Reply