ashton321 Posted November 19, 2009 Share Posted November 19, 2009 Hi, I have an array of file names to be used in another script created using glob() is there anyway to order the list by the date modified on the server? <?php $query = mysql_query("SELECT * FROM galleries"); $result = mysql_fetch_array($query); $galname = $result['galname']; $files = array(); foreach(glob("../webdev/images/galleries/$galname/*") as $file) { if(!is_dir($file)) { $files[] = array("path" => $file); } } // echo $files[0]["name"]; $firstfile = @ltrim($files[0]["path"], "../webdev/"); $firstfile = '../'.$firstfile; echo '<a href="../scripts/phpThumb.php?src='.$firstfile.'&h=400&w=400" rel="lightbox-'.$galname.'" ><img src="../scripts/phpThumb.php?src='.$firstfile.'&h=200&w=200"</a>'; foreach ($files as $pic) { foreach ($pic as $path) { if ($path != $files[0]["path"]) { $path = @ltrim($path, "../webdev/"); $path = '../'.$path; echo '<a href="../scripts/phpThumb.php?src='.$path.'&h=400&w=400" rel="lightbox-'.$galname.'" ></a>'; } else {} } } ?> Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/ Share on other sites More sharing options...
ashton321 Posted November 19, 2009 Author Share Posted November 19, 2009 Would i be better off just adding the file names to a database when uploaded? or can it be done with glob? Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/#findComment-961498 Share on other sites More sharing options...
Psycho Posted November 19, 2009 Share Posted November 19, 2009 Not tested: <?php $query = mysql_query("SELECT * FROM galleries"); $result = mysql_fetch_array($query); $galname = $result['galname']; $files = array(); foreach(glob('../webdev/images/galleries/$galname/*') as $file) { if(is_file($file)) { $files[] = array( 'path' => '../' . ltrim($file, '../webdev/'), 'date_modified' => filemtime($file); ); } } //Order the array by date modified function sortByModified() { if ($a['date_modified'] == $b['date_modified']) { return 0; } return ($a['date_modified'] < $b['date_modified']) ? -1 : 1; } usort($files, 'sortByModified'); //print the results echo "<a href=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" > echo "<img src=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=200&w=200\"</a>"; foreach ($files as $file) { echo "<a href=\"../scripts/phpThumb.php?src={$file['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" ></a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/#findComment-961526 Share on other sites More sharing options...
ashton321 Posted November 20, 2009 Author Share Posted November 20, 2009 Ok, I understand that(should have looked for the filetime function), now how would i apply that to my foreach loop that displays the result? I know if it were SQL you would date ORDER by date_modified DESC but i am assuming its not that simple for a foreach loop. I am just beginning to use foreach loops in my coding so im not exactly sure what to do. here is how i display the result currently <?php $firstfile = @ltrim($files[0]["path"], "../webdev/"); $firstfile = '../'.$firstfile; echo '<a href="../scripts/phpThumb.php?src='.$firstfile.'&h=400&w=400" rel="lightbox-'.$galname.'" ><img src="../scripts/phpThumb.php?src='.$firstfile.'&h=200&w=200"</a>'; foreach ($files as $pic) { foreach ($pic as $path) { if ($path != $files[0]["path"]) { $path = @ltrim($path, "../webdev/"); $path = '../'.$path; echo '<a href="../scripts/phpThumb.php?src='.$path.'&h=400&w=400" rel="lightbox-'.$galname.'" ></a>'; } else {} } } ?> Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/#findComment-961539 Share on other sites More sharing options...
Psycho Posted November 20, 2009 Share Posted November 20, 2009 In the code I provided, it would create a multi-dimensional array for each file which would include the path and the file modified date. Then there is a small block of code that would order the array. Then you just do a foreach loop to echo the results. As I stated that code was not tested - but the logic was sound. I fixed a few minor errors and it works fine now. Give this a try: <?php $query = mysql_query("SELECT * FROM galleries"); $result = mysql_fetch_array($query); $result['galname']; $files = array(); foreach(glob("../webdev/images/galleries/$galname/*") as $file) { if(is_file($file)) { $files[] = array( 'path' => $file, 'name' => basename($file), 'date_modified' => filemtime($file) ); } } //Order the array by date modified function sortByModified() { if ($a['date_modified'] == $b['date_modified']) { return 0; } return ($a['date_modified'] < $b['date_modified']) ? -1 : 1; } usort($files, 'sortByModified'); //print the results echo "<a href=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >"; echo "<img src=\"{$files[0]['path']}\"</a><br /><br />\n"; foreach ($files as $file) { echo "<a href=\"../scripts/phpThumb.php?src={$file['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >{$file['name']}</a><br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/#findComment-961678 Share on other sites More sharing options...
ashton321 Posted November 20, 2009 Author Share Posted November 20, 2009 Oh thank you very much! I didnt notice that it scrolled! sorry about that Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/#findComment-961829 Share on other sites More sharing options...
ashton321 Posted November 20, 2009 Author Share Posted November 20, 2009 Implemented it today, and it worked like a charm! Thanks for all the help Link to comment https://forums.phpfreaks.com/topic/182140-solved-order-array-of-files-made-with-glob/#findComment-962170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.