View Full Version : sorting arrays in php - then back to Flash
vman
December 19th, 2004, 11:36 PM
Hello everyone. I have a problem that I can't seem to solve. I have a completely dynamic imageviewer that creates a specific number of buttons based on how many folders are in a directory. I have php script that does this. I can't figure out how to arrange the array in alphabetical order before it is sent back to Flash. I ahve also tried sorting in Flash and have been unsuccessful. Here's is the script I have so far which is way off I know. Can someone give me a hand with this? Thanks.
<?
$dir = "Gigs";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if($filename != "." && $filename != "..") {
$foldernames=array($filename);
}
}
closedir($dh);
sort($foldernames);
foreach($foldernames as $val) {
echo "&foldernames=".$val;
}
?>
Here's a link to the image viewer:
http://thezplane.com/Imageviewer/
Jerryscript
December 20th, 2004, 12:45 AM
You need a method of incrementing the array, and then add the filenames to the array after the increment. Try something like this:
<?
$dir = "Gigs";
$dh = opendir($dir);
$foldernames=array();
$filecounter=0;
while (false !== ($filename = readdir($dh))) {
if($filename != "." && $filename != "..") {
$filecounter++;
$foldernames[$filecounter]=$filename;
}
}
closedir($dh);
sort($foldernames);
foreach($foldernames as $val) {
echo "&foldernames=".$val;
}
?>
vman
December 20th, 2004, 01:14 AM
Well,
I tried that but it returns:
&foldernames=01-12-04 TEST3&foldernames=01-22-04 TEST2&foldernames=04-17-04 TEST1&foldernames=TEST FOLDER 4&foldernames=TESTER1&foldernames=TESTER2&foldernames=TESTER3
I think I need something that returns:
&foldernames=01-12-04 TEST3, 01-22-04 TEST2, 04-17-04 TEST1, TEST FOLDER 4, TESTER1, TESTER2, TESTER3.
But it did sort them!
My php still sucks, otherwise I'd be able to take what you did and get what I need. I'll give it a shot in the AM.
Thanks for your help!
Jerryscript
December 20th, 2004, 06:38 AM
This should give you what you need:
<?
$dir = "Gigs";
$dh = opendir($dir);
$foldernames=array();
$filecounter=0;
while (false !== ($filename = readdir($dh))) {
if($filename != "." && $filename != "..") {
$filecounter++;
$foldernames[$filecounter]=$filename;
}
}
closedir($dh);
sort($foldernames);
echo "&foldernames=";
foreach($foldernames as $val) {
echo $val.", ";
}
?>
Happy Holidays! :)
vman
December 20th, 2004, 09:42 AM
Excellent! Thanks a lot. I appreciate it. Now all i have to do is reverse the order which I'm thinking should be fairly painless. I can probably do that in Flash.
Thanks again!
teiz77
December 20th, 2004, 11:29 AM
Excellent! Thanks a lot. I appreciate it. Now all i have to do is reverse the order which I'm thinking should be fairly painless. I can probably do that in Flash.
Thanks again!
use rsort instead of sort... This sorts in reversed order
vman
December 20th, 2004, 12:23 PM
Well, I already did something different before I read this:
<?
$dir = "Gigs";
$dh = opendir($dir);
$foldernames=array();
$filecounter=0;
while (false !== ($filename = readdir($dh))) {
if($filename != "." && $filename != "..") {
$filecounter++;
$foldernames[$filecounter]=$filename;
}
}
closedir($dh);
sort($foldernames);
$foldernames = array_reverse($foldernames);
echo "&foldernames=";
foreach($foldernames as $val) {
echo $val.",";
}
But I think your way is probably a couple microseconds faster!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.