multiple random background images?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kananook
Forum Newbie
Posts: 2
Joined: Mon Aug 19, 2002 1:27 am

multiple random background images?

Post by kananook »

is their a way with php, without using php images, where you can have 10-20 background images? one after the other? then is their a way to add a simple php script to make these images random?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

one after the other?
If there is a new request: yes. If you want it to cycle after a certain amount of time: no. But you might use a client-side script (like javascript) to do so or let the browser refresh the document after x seconds.
Both php and javascript could randomize the images.
Please clarify.
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Not sure if this is what you need, but try something like this..

Code: Select all

<body background=<?=$images[shuffle($images)]?>>
$images is the array the images are stored in. It will change randomly on reloads.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

void shuffle ( array array) does not return a random index.
Something like

Code: Select all

...
srand(srand ((float)microtime()*1000000));
<body background="<?php echo $images[rand(0, count($images)-1)]; ?>">
if the array has numerical indices without holes, or

Code: Select all

srand(srand ((float)microtime()*1000000));
<body background="<?php echo $images[array_rand($images)]; ?>">
is more advisable

http://www.php.net/manual/en/function.array-rand.php
Post Reply