This is something I just typed up. Not the same as the one I get to the other poster, but close.
It will read a folder and get the images. Then randomize them and display in a paginated form.
<?php
session_start();
$thispage = 'image.php';
$imageDir = 'icons/';
$perPage = 10; //total images to display per page
$perRow = 2; //total images per row
if ( isset( $_GET['reset'] ) ) {
unset( $_SESSION['chunks'] );
}
if ( !isset( $_SESSION['chunks'] ) ) {
if ( is_dir( $imageDir ) ) {
if ( $dir = opendir( $imageDir ) ) {
while ( ( $file = readdir( $dir ) ) !== false ) {
if ( $file !== '.' && $file !== '..' ) {
$images[] = $file;
}
}
closedir( $dir );
}
else {
die("Unable to read directory - {$imageDir}");
}
}
shuffle( $images );
$_SESSION['chunks'] = array_chunk( $images,$perPage );
}
$page = 1;
if ( isset( $_GET['page'] ) && $_GET['page'] > 0 ) {
$page = (int) $_GET['page'];
}
$chunk =& $_SESSION['chunks'];
$total = count( $chunk );
if ( isset( $chunk[$page-1] ) ) {
$images = $chunk[$page-1];
$html = "<table cellspacing=\"0\" cellpadding=\"3\">\n\t<tr>\n";
$i = 0;
foreach( $images as $image ) {
if ( $i == $perRow ) {
$html .= "\t</tr>\n\t<tr>\n";
$i = 0;
}
$html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
$i++;
}
$html .= "\t<tr>\n";
if ( $total > 1 ) {
$nav = '';
if ( $page > 1 ) {
$nav .= "<a href=\"{$thispage}?page=1\">[FIRST]</a><a href=\"{$thispage}?page=" . ( $page - 1 ) . "\">[PREV]</a>";
}
$i = 1;
while( $i < $total + 1 ) {
$nav .= "<a href=\"{$thispage}?page={$i}\">[{$i}]</a>";
$i++;
}
if ( $page < $total ) {
$nav .= "<a href=\"{$thispage}?page=" . ( $page + 1 ) . "\">[NEXT]</a><a href=\"{$thispage}?page={$total}\">[LAST]</a>";
}
$html .= "\t<tr>\n\t\t<td colspan=\"{$perRow}\">{$nav}</td>\n\t</tr>\n";
}
$html .= "</table><br /><br /><a href=\"{$thispage}?reset=true\">Shuffle Images</a>";
echo $html;
}
?>
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
Offline 1,315 posts
since Jun 2007