Order by date uploaded / created

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Order by date uploaded / created

Post 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!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

take a look at filemtime()
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

What would the function look like?
Post Reply