faqts : Computers : Programming : Languages : PHP : Sample Code

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

44 of 68 people (65%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do I sort a list of URLs on a page alphabetically?

Jul 19th, 2004 23:35
Alder Rus, Philip Olson, Matt Gregory, Deb bie, Onno Benschop, http://www.directoryonclick.com , http://www.singapore-yellow-pages.com


A few ways exist.
1. Urls from a SQL database
   Within the SELECT statement include the following :
     ORDER BY urls_title ASC
   ASC is default for ascending, DESC is used for descending order.
   An example QUERY might be :
     $q = "SELECT * FROM urls ORDER BY urls_title ASC";
     $r = mysql_query($r);
2. Urls from a file
   Assuming a file full of links exist, one link per line, we have a
   file that looks like this :
     // urls.txt
     http://www.google.com/
     http://www.php.net/
     http://www.apache.org/
   We can then open the file using file() where each array element 
   will automagically be a url.  So :
     $urls = file('urls.txt');
   http://www.php.net/manual/en/function.file.php
   This gives us an array full of urls, we can sort it many ways, sift 
   through the various sort functions in the manual :
   http://www.php.net/manual/en/function.sort.php
     $sorted_urls = sort($urls);
   Looping through with something like foreach can be done, printing 
   out our newly sorted urls :
     foreach ($sorted_urls as $url) {
       echo '<a href="'. $url .'">'. $url .'</a><br>';
     }