Sort by Last Updated (PLEASE, I'm so close!)

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

Sort by Last Updated (PLEASE, I'm so close!)

Post by VKX »

I just want these thumbnails to sort by the most recently uploaded. How do I do that?

Code: Select all

<?php
	$fileregex = "[gif]";				// Specify only .gif files
	$all = opendir('non-music/');		// Define image folder
	$photos = array();				// Define array

	// Fill the 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
	sort ($photos);                                         // THE CURRENT SORT METHOD!
	foreach ($photos as $i => $file) { 
		$url= substr($photos[$i],0,-4);
		$url = ltrim($url, "_");
		echo '<td><center><a href=http://www.awesomestart.com/'.$url.'/>
		<img src="non-music/'.$photos[$i].'" border="0"><br>';
		include("non-music/$url.php"); echo'</a></center></td>';
		$tablecount++;
		if ($tablecount == 4 && ($i+1) != $arlength) {
			echo'</tr><tr height="15"><td></td><td></td><td></td><td></td></tr><tr>';
			$tablecount = 0;
		}
	}
?>
Can I just change the "sort($photos);" line somehow?
Last edited by VKX on Fri Oct 14, 2005 1:55 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll have to sort them based on the output of filectime()
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Thanks for the constant help feyd, you're the best. Unfortunatly, I still need a little direction. How would I implement that change in my code?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

an array of filenames and an array of the filetime, use array_multisort() to sort the array of filenames by the corresponding times in the latter array
VKX
Forum Commoner
Posts: 41
Joined: Mon Oct 03, 2005 1:43 pm

Post by VKX »

Alright, I'm getting closer, but it's still not working. Here's my current script:

Code: Select all

<?php
		$fileregex = "[gif]";				// Specify only .gif files
		$all = opendir('gallery/music');	// Define image folder
		$photos = array();					// Define array
		$times = 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);
				}
			}
		}
		
		// Fill the time array:
		while ($file = readdir($all)) {
			if (!is_dir($location.'/'.$file) and $file != ".." and $file != ".") {
				if (preg_match($fileregex,$file)) {
					array_push($times,filectime($file));
				}
			}
		}
	
		array_multisort($times, $photos);
		$arlength1 = count($times);
		echo $arlength1;
		$arlength = count($photos);			// Number of images in folder
		echo $arlength;
	
		//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/music/'.$photos[$i].'" border="0" width="122" height="85"><br>';
			include("gallery/music/$url.php"); echo'</a></center></td>';
		}
	?>
The echos are showing me that the photos array does indeed include 52 values, but the times array doesn't include any. What could I be doing wrong?
Post Reply