display random images from folder
Hi guys...
I want some help of how to call images from a folder....
Its like..v have a folder called images and it has many images in it.....but i need only 6 images to display in a php webpage...when the webpage is reloaded, it displays another images...its like its displaying random images.....But in one page, i would like to display only 6 images....So, when the reload or visit again, the images or randomised from the images folder.....can any one know how can it be done....
I m not sure abt the codes...
Thanks
Tryphy
tryphy
Junior Poster in Training
79 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
ok sounds pretty easy. how do you want the images displayed on the page? in a table?
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
ok sounds pretty easy. how do you want the images displayed on the page? in a table?
Ya..in a table like in one row got 2 images...
so 3 rows and 2 columns...
tryphy
Junior Poster in Training
79 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
almost done, will post in a little while
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
tryphy
Junior Poster in Training
79 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
finished the code; i sent you a private message with the url of the test script
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
<?php
$arr=array();
for($i=0;$i<7;$i++){
$arr[$i]="./img/images/".$i.".jpg"; //assigning the image location to the array $arr.
}
$random=rand(0,6); //generating a random number
echo "<img src=".$arr[$random].">";
//printing the image
?>
Thats it :)
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
i think ezykiwi wanted to display more than one image. your code only displays one random image.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
if thats the case, why not generate one more random number ?
<?php
$arr=array();
for($i=0;$i<7;$i++){
$arr[$i]="./img/images/".$i.".jpg";
}
$random=rand(0,6);
$random_1=rand(0,6);
echo "<img src=".$arr[$random].">";
echo "<img src=".$arr[$random_1].">";
?>
This will do.. Don't you think ?
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
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><a href=\"{$thispage}?reset=true\">Shuffle Images</a>";
echo $html;
}
?>
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Replace
$html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";
with
$html .= "\t\t<td><a href=\"http://www.mysite.com/?v={$image}\"><img src=\"{$imageDir}{$image}\" /></a></td>\n";
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194