URL alphabetization
Moderator: General Moderators
URL alphabetization
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!!!
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!!!
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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.
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.
feyd | Please use
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]
[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);
?>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]- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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 guyandym01480 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.
if you can be more specific, that would be awesome. im pretty green to php.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
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>";
}
?>that's what i'm looking for! now when i look at what you've done, it makes sense to me.andym01480 wrote:worked on my server - although it sorts uppercase filenames into alphabetical and then lowercase - anyone any idea why???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>"; } ?>
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm