On one of my sites i have a folder of images and it uses this code to display them: <?PHP function createGallery($pathToImages, $pathToThumbs) { $output = "<table cellspacing=\"0\" cellpadding=\"0\" width=\"90%\">\n"; $output .= "<tr>\n"; $dir = opendir($pathToThumbs); $counter = 0; while (false !== ($fname = readdir($dir))) { if ($fname != '.' && $fname != '..') { $output .= "<td valign=\"middle\" align=\"center\" class=\"pictures\">\n <a href=\"#\" onClick=\"viewImage('{$pathToImages}{$fname}')\">\n"; $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />\n"; $output .= "</a>\n</td>\n"; $counter += 1; if ($counter % 4 == 0) {$output .= "</tr>\n<tr>";} } } closedir($dir); $output .= "</tr>\n"; $output .= "</table>\n"; $output .= "</body>\n"; $output .= "</html>\n"; echo $output; } createGallery("pictures/","pictures/thumbs/"); ?> PHP: the problem is it sorts the images completely randomly. Id like to have the images be sorted by name or date added. what is the code to do this? thanks!
Try this: <?PHP function createGallery($pathToImages, $pathToThumbs) { $files = array(); $output = "<table cellspacing=\"0\" cellpadding=\"0\" width=\"90%\">\n"; $output .= "<tr>\n"; $dir = opendir($pathToThumbs); $counter = 0; while (false !== ($fname = readdir($dir))) { if ($fname != '.' && $fname != '..') { $files[] = $fname; } } closedir($dir); sort($files); foreach ($files as $fname) { $output .= "<td valign=\"middle\" align=\"center\" class=\"pictures\">\n <a href=\"#\" onClick=\"viewImage('{$pathToImages}{$fname}')\">\n"; $output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />\n"; $output .= "</a>\n</td>\n"; $counter += 1; if ($counter % 4 == 0) {$output .= "</tr>\n<tr>";} } $output .= "</tr>\n"; $output .= "</table>\n"; $output .= "</body>\n"; $output .= "</html>\n"; echo $output; } createGallery("pictures/","pictures/thumbs/"); ?> PHP: