Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting while loops

Status
Not open for further replies.

tanveer91

Programmer
Nov 13, 2001
31
GB
Hi all,

Can someone please tell me how i can display contents of a directory in date order? (ie. earliest dates & onwards). Here's my code:

$handle=opendir('results');
while (($file = readdir($handle))!==false) {
if ( $file >= $date) {
include("results/$file");
}
}
closedir($handle);
 
try this:

$handle=opendir('results');
$array=array();
while (($file = readdir($handle))!==false) {
$array[$file]=date("Y-m-d h:i:s",filectime("results/$file"));


}
asort($array,SORT_STRING);

closedir($handle);
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thanks for the advice 'Anikin'.....sorry i forgot to include one more point, the files are already saved in date format '020220' so i won't need to get system date to sort it.

The problem is it seems to display the files in random order, and i would like to sort it from todays date and onwards.

Cheers
 
$handle=opendir('results');
$array=array();
while (($array[] = readdir($handle))!==false) {
}
asort($array,SORT_STRING); // for earlier to today or
arsort($array,SORT_STRING); // for today to earlier dates

closedir($handle);

i hope this will help you out
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Hi Anikin, tried the above method but it displayed php code on web browser!:

= $date) { print (" "); include("results/$array[]"); print ("
"); } } arsort($array,SORT_STRING); //for today to earlier dates closedir($handle); ?>


here's my code:

$date = date("dmy");
$handle=opendir('results');
$array=array();
while (($array[] = readdir($handle))!==false) {
if ( $array[] >= $date) {
include("results/$file");
}
}
arsort($array,SORT_STRING); //for today to earlier dates
closedir($handle);

Thanks for ur help...
 
Just a question are you interested in including files in order? if so read all the names to the array and then walk the array including all files.


$handle=opendir('results');
$array=array();
while (($array[]= readdir($handle))!==false); // i presume you don't have future dates so you can read everything
arsort($array,SORT_STRING); //sort the filenames
closedir($handle);

while(list(,$value)=each($array)){
include("results/$value");
}

as simple as this.

If shows code in the output is something wrong like you are closing PHP code in the wrong place.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top