Jump to content

[SOLVED] Determining the order...


JonW

Recommended Posts

Hi there,

 

I've got a script which acts as an image gallery, posting images found in a certain directory to the page.  My one problem is that I can't seem to figure out how the images are organized.  How is the order that it places the pictures determined?

 

I just want to group the images of one specific van together, while having all of the Econolines posted on one page.

 

<?php
$a = '0';
$filepath = "../wip/VanPictures/Ford/Econoline/thumb";
$url_path = "../wip/VanPictures/Ford/Econoline/main";
$dir = dir($filepath);
echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"2\" width=\"70%\">";
while($entry=$dir->read()) {
    if($entry == "." || $entry == "..") { 
        continue; 
    } 
    $fp = @fopen("$filepath/$entry","r");
if ($a % 2 == '0') {echo "<tr>";}
?><td>
  <a href="<? echo "$url_path/$entry \" rel=\"lightbox[econolineGallery]\"" ?>>
  <img src="<? echo "$filepath/$entry" ?>" border="0" alt="<? echo $entry ?>"></a>
  </td>
<?
$a = $a + 1;
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/
Share on other sites

The page displays all vans of a particular model (Econoline)

 

There are about 20 pictures, with about 5 unique Econoline vans with 4 pictures each.

 

I want the 4 pictures of one unique van to be shown in order, then the 4 of another unique van, etc.

 

I can't figure out how it's determining the order.  It doesn't look like filename, modified date, etc.

Well you are still not very specific :)

 

Do you have a dir structure like this:

 

VanPictures/Ford/

vanPictures/Volvo/

VanPictures/Opel/

 

or?

 

I haven't worked with the dir-class my self, I always use opendir() and readdir() but it should be pretty much the same never the less... Some time ago I wrote a script which would "batch rename" images (because I didn't want to do it manually) and I would like it to rename the images ordered by date so I did something like this:

 

#1: I looped over all the files in the directory and stored two pieces of information about each file - filename and last modified time...

<?php

$dir = 'some/path/';

$i = 0;
while (false !== ($file = readdir($handle))) {
    
    $files[$i]['modified'] = filemtime($dir . $file);
    $files[$i]['filename'] = $file;
        
    $i++;

}
?>

 

#2: I then sorted that array with asort($files) which ordered the array by the ['modified'] timestamp

 

#3: Now my files were ordered by modified date and I could do a foreach()

<?php
foreach($files as $file) {
    //Do something with the file
    echo "<img src='" . $dir . $file . "'>";
}
?>

 

 

Maybe you could do something similar. Store all the filenames in an array and then sort them.

Sorry for my incompetence, I've modified that to (hopefully) work with what I have, but I get my 20 Red X's :)

 

$file is being stored as "Array" so none of the paths are correct.

 

Edit:

 

Actually I think the problem is my attempted fix for the previous error where $handle is undefined and giving an error.  I looked at a php manual for readdir and changed it to:

 

<?php

$dir = '../wip/VanPictures/Ford/Econoline/thumb/';

$i = 0;

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {

    $files[$i]['modified'] = filemtime($dir . $file);
    $files[$i]['filename'] = $file;
        
    $i++;

}}
asort($files);

foreach($files as $file) {
    //Do something with the file
    echo "<img src='" . $dir . $file . "'>";
}

?>

 

Which I assume is wrong?

asort() sorts based on value. Since the value is a file returned from readdir(), I'm not sure how its being sorted.

 

You could use usort() and define a callback function that compares the files based on something you determine.

 

Also, you are trying to access the filename as $file, but you are populating the filename into an array.

If you do this, you need to access the filename by its array index ei:

$file["filename"];

 

function compareByName($a, $b){
   return strcmp($a["filename"], $b["filename"]);
}

function compareByDate($a, $b){
   return ($a["modified"] < $b["modified"]) ? -1 : 1;
}


$dir = '../wip/VanPictures/Ford/Econoline/thumb/';

$i = 0;

if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {

    $files[$i]['modified'] = filemtime($dir . $file);
    $files[$i]['filename'] = $file;
        
    $i++;

  }
}

usort($files, "compareByDate");

foreach($files as $file) {
    //Do something with the file
    echo "<img src='" . $dir . $file["filename"] . "'>";
}

As I said, I have only posted pieces of code from an old renaming script to illustrate the idea...

 

asort() sorts by value yes and that was smart when I has an array containing the file's last modified date - he would of course have to use something else :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.