JonW Posted August 27, 2007 Share Posted August 27, 2007 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/ Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 What you want to do - group together by one specific van - is very vaguely described... how do you want to group them? Each sub folder of VanPictures together or? Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335495 Share on other sites More sharing options...
JonW Posted August 27, 2007 Author Share Posted August 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335517 Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335534 Share on other sites More sharing options...
JonW Posted August 27, 2007 Author Share Posted August 27, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335570 Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 As I said you use the dir-class to read your files and I use a opendir() combined with readdir(). All I did was suggesting a method. You can use your own code to obtain the same result. Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335588 Share on other sites More sharing options...
socratesone Posted August 27, 2007 Share Posted August 27, 2007 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"] . "'>"; } Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335641 Share on other sites More sharing options...
Wuhtzu Posted August 27, 2007 Share Posted August 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66909-solved-determining-the-order/#findComment-335691 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.