Resend my activation email : Register : Log in 
BCF: Bike Chat Forums


[PHP] display files in order

Reply to topic
Bike Chat Forums Index -> The Geek Zone
View previous topic : View next topic  
Author Message

powelly
World Chat Champion



Joined: 29 May 2007
Karma :

PostPosted: 15:37 - 06 Feb 2009    Post subject: [PHP] display files in order Reply with quote

I have a webcam that uploads an image to a folder every minute, this files are named as follows:

snapshot-2009-02-02-18-25-02.jpg

Im using the following code to display all the images from a given date (currently hardcoded as $date)

Code:
<?php
$date = "2009-02-02";
if ($handle = opendir('../../webcam/')) {
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
         if (eregi('2009-02-02', $file)) {
      
            $page_name = $file;
               //$page_name=substr($file, 0, strpos($file, "."));
               echo "<img src='../../webcam/$page_name' width='160'/>";
      
         }
      
      }
   }
}
closedir($handle);
?>


However the output does not display the images in order, is there anyway to get round this based on either the filename or the files timestamp?
____________________
Yamaha RXS 100 > Gilera Runner 50 > Suzuki SV650SY
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 15:59 - 06 Feb 2009    Post subject: Reply with quote

Hi

I would do your directory command and stick the results into an array and then look through the array to output it.

Eg.

Code:

<?php
$date = "2009-02-02";
$pages=array();
if ($handle = opendir('../../webcam/')) {
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
         if (eregi('2009-02-02', $file)) {
       
            $pages[] = $file;
         }
       
      }
   }
}
rsort($pages);
foreach($pages as $page_name)
{
               echo "<img src='../../webcam/$page_name' width='160'/>";
}

closedir($handle);
?>


Not tried test running it but that should work.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

rc211v
Nitrous Nuisance



Joined: 08 Oct 2007
Karma :

PostPosted: 16:27 - 06 Feb 2009    Post subject: Reply with quote

Code:
while (false !== ($file = readdir($handle)))


that's a weird way of constructing a conditional statement.

I always advice my students from doing it this way, I don't know why though.

Anyway, yeah, same as mentioned above. Go through the directory and place the filenames into an array. You can then sort the array, and use it to display your images in order.

I don't know if PHP comes with a built-in sorting function, but there are lots of algorithms out there you can use. Bubble sort maybe? Mr. Green
____________________
VFR400 (NC30)
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 16:50 - 06 Feb 2009    Post subject: Reply with quote

rc211v wrote:
Code:
while (false !== ($file = readdir($handle)))


that's a weird way of constructing a conditional statement.


It is a bit strange but a few things seem to do it that way including on the php help pages.

rc211v wrote:
I don't know if PHP comes with a built-in sorting function, but there are lots of algorithms out there you can use. Bubble sort maybe? Mr. Green


Bloomin loads of them depending on what you want to sort.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

powelly
World Chat Champion



Joined: 29 May 2007
Karma :

PostPosted: 22:33 - 06 Feb 2009    Post subject: Reply with quote

Kickstart wrote:
Hi

I would do your directory command and stick the results into an array and then look through the array to output it.

Not tried test running it but that should work.

All the best

Keith


Worked perfectly, Thumbs Up
____________________
Yamaha RXS 100 > Gilera Runner 50 > Suzuki SV650SY
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts
Old Thread Alert!

The last post was made 17 years, 231 days ago. Instead of replying here, would creating a new thread be more useful?
  Display posts from previous:   
This page may contain affiliate links, which means we may earn a small commission if a visitor clicks through and makes a purchase. By clicking on an affiliate link, you accept that third-party cookies will be set.

Post new topic   Reply to topic    Bike Chat Forums Index -> The Geek Zone All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Read the Terms of Use! - Powered by phpBB © phpBB Group
 

Debug Mode: ON - Server: birks (www) - Page Generation Time: 0.10 Sec - Server Load: 0.57 - MySQL Queries: 14 - Page Size: 49.33 Kb