Page 1 of 1

Order by date uploaded / created

Posted: Fri Dec 02, 2005 11:06 am
by VKX
I have a list of thumbnails on the main page of AwesomeStart.com. Here's the code I use to display them.

Code: Select all

<?php
		$fileregex = "[gif]";				// Specify only .gif files
		$all = opendir('gallery/Movie');	// Define image folder
		$photos = array();					// Define array
	
		// Fill the photo array:
		while ($file = readdir($all)) {
			if (!is_dir($location.'/'.$file) and $file != ".." and $file != ".") {
				if (preg_match($fileregex,$file)) {
					array_push($photos,$file);
				}
			}
		}
		
		$arlength = count($photos);			// Number of images in folder
	
		//The Display Loop
	    for ($i=$arlength-1; $i>$arlength-5; $i--) { 
			$url= substr($photos[$i],0,-4);
			$url = ltrim($url, "_");
			// "The" Exceptions
			if ($url == 'birthdaymassacre') {
				$url = 'tbm'; }
			if ($url == 'faint') {
				$url = 'thefaint'; }
			if ($url == 'beatles') {
				$url = 'thebeatles'; }
			echo '<td><center><a href=http://www.awesomestart.com/'.$url.'/>
			<img src="gallery/Movie/'.$photos[$i].'" border="0" width="122" height="85"><br>';
			include("gallery/Movie/$url.php"); echo'</a></center></td>';
		}
	?>
That particular batch of code displays the four movie thumbnails on the main page. What do I have to add to it in order to display the four NEWEST (most recently uploaded, updated, whatever) images?

Thanks for the help!

Posted: Fri Dec 02, 2005 9:19 pm
by Burrito
take a look at filemtime()

Posted: Sat Dec 03, 2005 6:46 pm
by VKX
I've managed to extract the time using this code:

Code: Select all

$url= substr($photos[$i],0,-4);
			$url = ltrim($url, "_");
			$fullurl = "../images/_$url.gif";
			echo '<td><center><a href=http://www.awesomestart.com/'.$url.'/>
			<img src="Movie/'.$photos[$i].'" border="0"><br>'; include("Movie/$url.php"); echo'</a>&nbsp;'; echo filemtime($fullurl); echo'</center></td>';
and echo it right after the file name (for testing purposes.) Now I just need to figure out how to sort these by this number rather than the alaphanumeric way I've been doing it:

Code: Select all

sort ($photos);
Any suggestions?

Posted: Sat Dec 03, 2005 8:07 pm
by John Cartwright

Code: Select all

if (preg_match($fileregex,$file)) {
                    array_push($photos,$file);
                }
Instead of storing the $file in the array, create a 3d array which holds the result from filemtime() aswell as the filename.. ie

Code: Select all

if (preg_match($fileregex,$file)) {
                    $photos[] = array(
                          'filename' => $file,
                          'lastModified' => filemtime($file)
                    );
                }
and use usort() to sort the array.

Posted: Sat Dec 03, 2005 8:18 pm
by VKX
What would the function look like?