TeamPhotoshop
Reviews, updates and in depth guides to your favourite mobile games - AppGamer.com
Forum Home Latest Posts Search Help Subscribe

random images

Page: 1 Reply
Feb 7th 2004#141212 Report
Member since: Jan 19th 2004
Posts: 10
hey guys, just wondering if anyone knows what the code is for this ;

say i have a few images on the frontend of my website, and i would like the image to change everytime the website refreshed.

thx
Reply with Quote Reply
Feb 7th 2004#141214 Report
Member since: Jul 19th 2003
Posts: 810
It would look something like this :

[quote]


[/quote]

I have used this for quite a long time..... and it works well for me.....

only two things to say really :
1) make sure all the paths to your imagesa are correct
2) make sure your iamges are all the same size EXACTLY!!!

and finally.... you can have as many images as you want to have.... just add them into the array
Reply with Quote Reply
Feb 11th 2004#141817 Report
Member since: Jan 19th 2004
Posts: 10
sorry can't see anything in ur post?!?
Reply with Quote Reply
Feb 11th 2004#141848 Report
Member since: Dec 9th 2003
Posts: 180
I have what you described on one of my sites (using client side code)...it's kinda messy but it works....

code in index file
[html]

[/html]

code in my JS file
[html]
//...
function selectsplash()
{
img=selectsplash.arguments[Math.floor((Math.random() * selectsplash.arguments.length))]
}
//...
function writeasplash()
{
selectsplash("1.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg","9.jpg","31.jpg");
document.write("");
}
[/html]
Reply with Quote Reply
Feb 11th 2004#141852 Report
Member since: Jul 19th 2003
Posts: 810
Talos.... i have a small chunk of code if you want it .... write me an email as i cant get the code to work in here..... for some reason
Reply with Quote Reply
Feb 11th 2004#141858 Report
Member since: Mar 29th 2003
Posts: 1326
I'd use PHP for this. Add my image paths on separate lines in a text file, and

[php]
$arrImages = file('the_text_file.txt');
$iRand = rand(0, count($arrImages)-1);
echo 'something';
?>
[/php]

At least I think that should work ;) And if the images are different sizes, then just store the whole 'img src...' line in the text file and just go echo $arrImages[$iRand]; instead of all that html. Good luck.
Reply with Quote Reply
Feb 12th 2004#141889 Report
Member since: Nov 26th 2001
Posts: 2586
You could also (instead of using that text file) just gather a bunch of images from a directory into an array, then use the count(array) to get the random number....

This would at least make it so all you need to do is upload the images into that dir and never have to edit a path file. (hmmm saves about 10 seconds on your life. Equiv to maybe one cigarette? - so at least you can smoke a cigarette and not have to worry that you just had to edit a text file.) :rolleyes:

[php]
$path = "./path/to/hiZZouse/";
$num = 0;

if ( is_dir($path) ) {

if ( $rand_dir = opendir($path) ) {

while ( $img = readdir($rand_dir) !== false ) {

$img_array[$num] = $img; // or if you want a full path:
// $img_path[$num] = $path."/".$img;

} # end while

closedir($rand_dir);

} # end if

} # end if
[/php]

That should work if I didn't bung it up.
Reply with Quote Reply
Feb 12th 2004#141904 Report
Member since: Sep 29th 2003
Posts: 1496
I use the one marble uses, but I can only get it to work every once and a while, I am horrible with coding. :p
Reply with Quote Reply
Feb 12th 2004#141907 Report
Member since: Apr 20th 2002
Posts: 3000
Or you can make it so that it sets a cookie so that the same splash won't come back two times in a row:

[php]

$foldername="splashes";

if ($handle = opendir($foldername)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (eregi(".jpg$",$file) or eregi(".gif$",$file)) {
$filelist[] = $file;
}
}
}
closedir($handle);
}

$splash=$filelist[rand(0, count($filelist)-1)];
while (isset($_COOKIE['splash']) && eregi($link, $_COOKIE['splash'])) {
$splash=$filelist[rand(0, count($filelist)-1)];
}
setcookie('splash', $splash, time()+30);

echo "\"\"";
?>
[/php]
Reply with Quote Reply
Feb 12th 2004#141914 Report
Member since: Nov 26th 2001
Posts: 2586
One thing I forgot (and might be the reason it isn't showing up sometimes.) is with *nix machines when you do a file list you will get the "." and ".." files. So you will have to run a check in the while loop:

[php]
while ( $img = readdir($rand_dir) !== false ) {

if ( $img != "." || $img != ".." ) {

$img_array[$num] = $img; // or if you want a full path:
// $img_path[$num] = $path."/".$img;

} # end while
}
[/php]
Reply with Quote Reply
Page: 1 Back to top
Please login or register above to post in this forum