Current location: Hot Scripts Forums » Programming Languages » PHP » Alphabetical Order


Alphabetical Order

Reply
  #1 (permalink)  
Old 08-14-04, 11:35 PM
webomen webomen is offline
New Member
 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Alphabetical Order

I would like to know if it's possible with PHP to sort out a list of .txt files on a hardrive, in to an Alphabetical Index Page, with the first letter of the .txt file example: "A", into a seperate page with all the files starting with the letter "A", and so on. What function would be at use here?
Reply With Quote
  #2 (permalink)  
Old 08-16-04, 03:25 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Sure it's possible -- have you tried anything? Check the manual for functions like readdir() and sort(). There are quite good examples with them also.
Reply With Quote
  #3 (permalink)  
Old 08-16-04, 06:00 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah... darkfreak is right... do you have any basic php skillz?

You might also want to check out some information about Arrays om php.net
__________________
**Schmmotch**
Reply With Quote
  #4 (permalink)  
Old 08-16-04, 12:45 PM
webomen webomen is offline
New Member
 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thx guys, I've a little bit of skillz, even took classes for a while but have to remember it again...I'll look it up

do you've any examples of it?
Reply With Quote
  #5 (permalink)  
Old 08-16-04, 02:17 PM
webomen webomen is offline
New Member
 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Would this work to assign each individual file to an array, ranging
from the number of files in the folder?

Code:
<?php

if ($handle = opendir('images')) {
   echo "Files:\n";

   while (false !== ($file = readdir($handle))) { 
   if ($file != "." && $file != "..") {
   $index = count($file);
   for($counter=0; $counter < $index; $counter++)  
{
$myArray = array($counter=>$file);
  print "$myArray[$counter] ";
}

	   }
   }
   closedir($handle); 
}

?>
Reply With Quote
  #6 (permalink)  
Old 08-17-04, 12:46 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Altough it's working somehow, your code has some flaws. For example, you don't actually need the count() at all (it gets the size of an array and you use it on string -- the filename -- which will always return 1), and you have to do some more thinking when outputting the filenames.

Please see my version of the code and try to see what I have changed. Hopefully it will get you started!

PHP Code:

<?php

   $dir 
".";
   
$filenames = array();

   if (
$handle opendir($dir)) {
       
/* read every entry from the given directory */
       
while (false !== ($file readdir($handle))) {
         
/* check if we got a valid entry (file or dirname) */
         
if ($file != "." && $file != "..") {
             
/* append current filename to array */
             
$filenames[]=$file;
     }

   }
   
closedir($handle);

   
/* show what we got */

   
$numfiles count($filenames);
   echo 
"total of $numfiles files.<br>\n";

   echo 
"<pre>";
   
print_r($filenames);
   echo 
"</pre>";

   
sort($filenames);

   echo 
"<pre>";
   
print_r($filenames);
   echo 
"</pre>";
}

?>

Last edited by darkfreak; 08-17-04 at 01:08 AM.
Reply With Quote
  #7 (permalink)  
Old 08-17-04, 01:03 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Oh, one more thing: check the functions is_dir() and is_file() for getting just the filenames and no directorynames. Or, with these you can make the code 'recursive' so that it will get all the filenames from all the directories below the given one -- that's your next challenge
Reply With Quote
  #8 (permalink)  
Old 08-18-04, 12:13 PM
webomen webomen is offline
New Member
 
Join Date: Aug 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thx, mate this is exactly what I wanted for the array. I'll look at those functions as well. The only thing I'm interested now is to declare the arrays with the individual starting letters into a seperate page.
Reply With Quote
  #9 (permalink)  
Old 08-19-04, 01:41 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Here's a little addition to the previous code -- it will create a new array with alphabet keys and adds each filename in it's corresponding subarray in the main array.

PHP Code:

   /* sort filenames alphabetically in ascending order */

   /* SORT_NUMERIC seems to ignore character case */
   
sort($filenamesSORT_NUMERIC);

   
$array = array();

   
/* add each filename to the array by it's first letter */
   
foreach($filenames as $filename) {
       
$firstletter strtolower(substr($filename01));
       
$array[$firstletter][] = $filename;
   }

   
/* sort the new array by it's keys (alphabets) */
   
ksort($array);

   
/* show the results */
   
echo "<pre>";
   
print_r($array);
   echo 
"</pre>"
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
!!!~~[Need Order Forum]~~!!!~~Willing To PAY!!!!! barbesho Job Offers & Assistance 2 07-13-04 04:42 PM
too simple to find, script allowing upload to order form kustommf Script Requests 1 07-02-04 07:30 PM
Soon..Need PHP Coder to MOD 'shopping cart order system' superprogrammer Job Offers & Assistance 2 02-09-04 07:29 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM
Sort order of entries Skeleton Man Hot Scripts Forum Questions, Suggestions and Feedback 21 06-14-03 11:32 PM


All times are GMT -5. The time now is 04:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.