melloorr Posted December 15, 2011 Share Posted December 15, 2011 Hey everyone. I have been trying to assign dates to image so I could sort them, and also display the date they were uploaded. But I need some help. I have a script that uploads an image to a file directory, and also stores the file directory and the upload date in a database. And I also have a script that displays the images in the file directory but I cannot find a way to assign the right date to the right image. Here is the script that displays the images: if (isset($_POST['submit'])) { // fill an array with all items from a directory $dir = "thumbs/"; $handle = opendir($dir); while ($file = readdir($handle)) { $files[] = $file; // This removes the full stops from the array unset($files[0]); unset($files[1]); } foreach($files as $fileimg) { echo "<img src='". $dir . $fileimg ."'/>\n"; echo "$date"; } } and here is the script for calling the date from the database: $query = mysql_query("SELECT imgaddress, uploaddate FROM thumbs WHERE imgaddress='" . $dir . $file . "'"); if (mysql_num_rows($query) == 1) { $row = mysql_fetch_assoc($query); $date = $row['uploaddate']; If I use the database statement before the foreach, then it does not get the right file directory, but if I call it inside the foreach, then I cannot use krsort() to sort the array. Any help would be greatly appreciated, thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 15, 2011 Share Posted December 15, 2011 Why are you trying to use both file handler functions AND the database? If you want to use file handler functions, then use those to read the date of the file. Or, better yet, just use the database values to display the output. If you have records for all the images, there is no need to read the files from the directory! Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 15, 2011 Share Posted December 15, 2011 if (isset($_POST['submit'])) { $query = "SELECT imgaddress, uploaddate FROM thumbs ORDER BY uploaddate"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo "<img src='{$row['imgaddress']}'/>\n{$row['uploaddate']}<br>\n"; } } Quote Link to comment Share on other sites More sharing options...
melloorr Posted December 15, 2011 Author Share Posted December 15, 2011 Why are you trying to use both file handler functions AND the database? If you want to use file handler functions, then use those to read the date of the file. Or, better yet, just use the database values to display the output. If you have records for all the images, there is no need to read the files from the directory! Now I feel pretty stupid because it seems so obvious :-\ Thanks you for all the help Quote Link to comment 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.