whatwhat123 Posted January 17, 2009 Share Posted January 17, 2009 Hi, Im new to php and have been trying to put together a simple image hosting site with code that ive found around the internet. I've got things working but I want the images in the gallery to display the latest uploaded first. This is the code im using to display the images <html> <head> <title>Slideshow</title> <style> </style> </head> <body> <?php require 'config.php'; $offset=0; if(isset($_GET['offset'])) { $offset=$_GET['offset']; } $rowcount=1; $perc=round((1/$imgperrows)*100); ?> <table width="100%"> <tbody> <tr> <?php $d=dir("files"); $count=0; while (false !== ($entry = $d->read())) { if($entry!='.' && $entry!='..') { if($count>=$offset && $count<($offset+$imgperpage)) { if($useshrinked) { if(file_exists('tmb/'.$tmbprefix.$entry)) { echo '<td width="'.$perc.'%" align="center"><a href="images/'.$entry.'"'; if($launchnew) echo ' target="_blank"'; echo '><img src="tmb/'.$tmbprefix.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>'; } else { echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"'; if($launchnew) echo ' target="_blank"'; echo '><img src="files/'.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>'; } } else { if(file_exists('tmb/'.$tmbprefix.$entry)) { echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"'; if($launchnew) echo ' target="_blank"'; echo '><img src="tmb/'.$tmbprefix.$entry.'" width="80" height="80" border="0"><br>'.$entry.'</td>'; } else { echo '<td width="'.$perc.'%" align="center"><a href="files/'.$entry.'"'; if($launchnew) echo ' target="_blank"'; echo '><img src="noprev.gif" width="80" height="80" border="0"><br>'.$entry.'</td>'; } } $rowcount++; if($rowcount>$imgperrows) { echo '</tr><tr>'; $rowcount=1; } } $count++; } } $d->close(); ?> </tr> </tbody> </table> <br> <center> <input type="button" value="Previous" style="font: 11px bold arial;width:70px;"<?php if($offset==0) { ?> DISABLED<?php } ?> onClick="window.location='index.php?offset=<?php echo $offset-$imgperpage; ?>';"> <input type="button" value="Next" style="font: 11px bold arial;width:70px;"<?php if($count>$offset && $count<=($offset+$imgperpage)) { ?> DISABLED<?php } ?> onClick="window.location='index.php?offset=<?php echo $offset+$imgperpage; ?>';"> </center> The uploading code im using creates a log file with upload times in it. Can anyone give me some help and point me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/141254-making-the-latest-images-in-a-simple-gallery-display-first/ Share on other sites More sharing options...
DeanWhitehouse Posted January 17, 2009 Share Posted January 17, 2009 If you have the time added stored then in the SQL query add ORDER time_added DESC replace time_added with the name of the column that contains the time the image was added. Also please use code tags Quote Link to comment https://forums.phpfreaks.com/topic/141254-making-the-latest-images-in-a-simple-gallery-display-first/#findComment-739347 Share on other sites More sharing options...
whatwhat123 Posted January 17, 2009 Author Share Posted January 17, 2009 Thanks for the quick reply. Im not usiing and SQL database though I just have the images in a folder. Upload times are saved in a text file. Will it make things much more diffucult doing it without an SQL database? Quote Link to comment https://forums.phpfreaks.com/topic/141254-making-the-latest-images-in-a-simple-gallery-display-first/#findComment-739352 Share on other sites More sharing options...
DeanWhitehouse Posted January 17, 2009 Share Posted January 17, 2009 O right, just read that erm yeah it will make things a bit more complicated. What i would advise then is to maybe store the images and times in an array and sort the array by time, i am not %100 on that though. Quote Link to comment https://forums.phpfreaks.com/topic/141254-making-the-latest-images-in-a-simple-gallery-display-first/#findComment-739355 Share on other sites More sharing options...
dawsba Posted January 18, 2009 Share Posted January 18, 2009 <? function sortarrayByDate($array,$dir) { $temp = $array; $temp2 = array(); $targetdir = getcwd()."/".$dir; foreach($temp as $key => $i){ $modifyDate = floatval(date("YmdHis", (filemtime($targetdir.$i)))); if($key < 10){ $temp2[$i] = floatval($modifyDate."0000".$key); } elseif($key < 99){ $temp2[$i] = floatval($modifyDate."000".$key); } elseif($key < 999){ $temp2[$i] = floatval($modifyDate."00".$key); } elseif($key < 9999){ $temp2[$i] = floatval($modifyDate."0".$key); } else{ $temp2[$i] = floatval($modifyDate.$key); } } arsort($temp2); $counter = 0; foreach($temp2 as $key => $value){ $temp[$counter] = $key; $counter++; } return $temp; } $targetdir = getcwd()."/docs"; $files = array(); $directory = opendir($targetdir); while($filename = readdir($directory)) { if(strlen($filename) > 2) { array_push($files, $filename); } } $files = sortarrayByDate($files,'docs'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141254-making-the-latest-images-in-a-simple-gallery-display-first/#findComment-739412 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.