Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 14th, 2003, 09:32 AM
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting an array

Hello people. `

As a rookie, I'm having problems with Asort(). In the script below I open a directory, read all the html files in this directory, then grabbing line 5 out of each one of them to create a link to them. the problem is I'm having a hard time sorting the links alphabeticaly.

<?php
$default_dir = "./";
function traverse_dir($dir) {
   chdir($dir);
   if(!($dp = opendir($dir))) die("Can't open $dir.");
   while($file = readdir($dp)) {
   if(substr($file, -5)== ".html"){
   $filelines = file($file);
   $links = $filelines[4];
   $links = ereg_replace("<TITLE>", "", $links);
   $links = ereg_replace("</TITLE>", "", $links);
   echo "<a href=\"".$file."\">".$links."</a><BR>";
    }}
   closedir($dp);
}
?>

how would someone sort $link variable in this case? I tried alot of things, and something seems to escapes me because I cant seem to get it.

Reply With Quote
  #2 (permalink)  
Old October 14th, 2003, 01:16 PM
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks guys but I got it: (well maybe I cheated... but like I said I'm a rookie)

<?php
$default_dir = "./";
function traverse_dir($dir) {
$counter = 0;
$ordered_links = array();
   chdir($dir);
   if(!($dp = opendir($dir))) die("Can't open $dir.");
   while($file = readdir($dp)) {
   if(substr($file, -5)== ".html"){
   $filelines = file($file);
   $links = $filelines[4];
   $links = ereg_replace("<TITLE>", "", $links);
   $links = ereg_replace("</TITLE>", "", $links);
   $links = ereg_replace("\\\\n", "", $links);
   $ordered_links[$counter] = $links."@".$file;
   $counter++;
   //echo "<a href=\"".$file."\">".$links."</a><BR>";
    }}
   closedir($dp);
   asort($ordered_links);
   foreach ($ordered_links as $subject){
   $file = explode("@", $subject);
   echo "<a href=\"".$file[1]."\">".$file[0]."</a><BR>";
   }
}
?>

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting Array in C++ code_lover C++ Programming 4 October 16th, 2008 01:43 AM
Sorting 2 dimensional array rgalehouse Javascript How-To 2 December 2nd, 2006 10:46 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
sorting two dimensional array erin VBScript 0 February 10th, 2004 05:55 PM
Array Sorting Dinesh22 VB.NET 2002/2003 Basics 2 February 3rd, 2004 11:19 AM



All times are GMT -4. The time now is 06:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.