954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 

Thanks...

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
 

i posted exactly same one as you want 3 days ago.

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

Im new to the forums here but was just wondering if there is any chance of being able to look at the code? im looking for something similar to this but all the examples i have found online are for only displaying 1 random image at time...

Cheers Ezy

ezykiwi
Newbie Poster
2 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 
<?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!
Moderator
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!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

that looks like it could do the trick with abit of working... basically....

i want there to be a folder of images... the names of those images could be anything at all.... number letters.. mixtures... there will be no logical order to them..... i just need the code to randomly pick say 10 images... out of that folder... no image being displayed twice etc...

if there isnt enough ... say there is 4 images.. it will just display 4

if there is no images... maybe just a statement showing there is no images to display at this time...

ezykiwi
Newbie Poster
2 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 
finished the code; i sent you a private message with the url of the test script


Hi, I am fairly new to web design and am trying to do exactly what you helped the other person on this post do, is there any way I could also get ahold of that code? I would be very grateful. Thanx.

WebServant
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

Hi,

I am new to Php. Iam unable to write a php code for random images on a webpage that displays new image each time it is refreshed. Im having images in a folder in my college FTP. Can any one post a code for me in Php ? Coz im unable to make the changes to my php file....

sreeneel
Newbie Poster
1 post since Sep 2008
Reputation Points: 10
Solved Threads: 1
 

plz post the script so that we understand what u did

angelic_devil
Newbie Poster
24 posts since May 2009
Reputation Points: 10
Solved Threads: 1
 

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
 

Hi..kkeith29
How can we make the image click able ?
wrt line 48 in your code

$html .= "\t\t<td><img src=\"{$imageDir}{$image}\" /></td>\n";

I tried

$html .= "\t\t<td><a href="MYSITE.com/?v={$image}"<img src=\"{$imageDir}{$image}\" /></td>\n";


But I got error..
can you please help me to make the images displayed clickable in this format
mysite.com/?v={$image}

Thanks :D

poison23
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

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
 

Thanks :)

poison23
Newbie Poster
2 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

Can I put this in an asp.net page?

I'm not sure where to put this php code.. ? Sorry

keltoid
Newbie Poster
19 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: