try this:
<html>
<title>test</title>
<body>
<?php
$images = "images/"; # Location of galleries
$cols = 4; # Number of columns to display
$max = 5; # Maximum number of galleries to show
if ($handle = opendir($images)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
closedir($handle);
}
natcasesort($files);
$colCtr = 0;
echo "<div id=\"updates\"><table><tr>";
$c = count($files); //$c = the number of files in the array, minus 1 = highest index in the array.
$start = ($_GET['page'] > 1)? ($_GET['page'] - 1)*$max : 1;
$end = (($c - $max) > 0)? ($c - $max)$max - ($c));
for($i = $c - $start; $i >= $end; $i--) //$i = $c, $i is greater than the array count minus $max, $i de-increments on each loop. This will give us a countdown for the number of galleries required.
{
if($colCtr %$cols == 0)
echo "</tr><tr>";
echo "<td><img src=\"" . $images . $files[$i] . "/head_bg_form.gif\" width=\"240px\" height=\"180px\" alt=\"" . $alt . "\" /></td>"; //echo'ing out the $file[$i] on the loop, will give us the last required number of files in the file array.
$colCtr++;
}
echo "</table></div>" . "\r\n";
//pagination
echo '<table><tr>';
$nbpages = ceil(($c+1)/$max);
$current_page = isset($_GET['page'])? $_GET['page'] : 1;
for($page=1; $page <= $nbpages; $page++){
if($page == $current_page){
echo '<td><span>'.$page.'</span></td>';
}else{
echo '<td><a href="?page='.$page.'"><span>'.$page.'</span></a></td>';
}
}
echo '</tr></table>';
?>
</body>
</html>
It's better to use natcasesort() than sort(). Otherwise 002 will be greater than 012.