Random image and table color on the welcome screen?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
FliesLikeABrick
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Nov 27, 2003 9:54 pm
Location: NY
Contact:

Sat Nov 29, 2003 6:20 am

im thinking the concept of it could be as simple as choosing a random number and basing it on that....restrict the output of your random number to say, 10, because thats how many images you have, and do the same with color. then you can say
if ($random=10)
etc etc

and put a conditional for each color and image

at least thats what i would think to try

User avatar
pio
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Nov 29, 2003 9:58 am
Location: Oslo, Norway
Contact:

Sat Nov 29, 2003 10:50 am

I would do something like this:

If you want to randomize two pictures and the table background-color, make three arrays:

$pic1 = array('image1.gif','image2.jpg','image2.png');
$pic2 = array('image4.gif','image5.jgp','image6.png');
$color = array('#FF0000','#00FF00','#0000FF',);

of course you will use your own filenames and colors. Just make sure there is an equal amount of entries in all of the arrays. The pictures should have the same position in the arrays as the background-color you want has in the $color-array. Above, image2 and image5 will get bg-color #00FF00...

Then, on page load (put the code in the HEAD-section of the document) you can get random, but corresponding, images and colors using PHP's rand()-function:

$number = rand(0,2);

This will return a random integer between and including 0 and 2, which is good since our arrays has three entries each and starts counting at zero. We can now fetch the filenames and color-value using the $number-variable:

$bgcolor = $color[$number];
$img1 = $pic1[$number];
$img2 = $pic2[$number];

In the BODY of the document, you can now output the data like this:

<table bgcolor="<?php echo $bgcolor; ?>">
<tr><td>
<img src="your.web.site/yourfolder/<?php echo $img1; ?>">
<img src="your.web.site/yourfolder/<?php echo $img2: ?>">
</td></tr>
</table>

If you want to preload your pictures with javascript, you can do this like you would with an ordinary HTML-page, without any relation to the PHP code..

Hope this is any help :)
Last edited by pio on Sat Nov 29, 2003 10:55 am, edited 1 time in total.

User avatar
pio
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Nov 29, 2003 9:58 am
Location: Oslo, Norway
Contact:

Sat Nov 29, 2003 10:54 am

put this in the HEAD-section:

<?php

$pic1 = array('image1.gif','image2.jpg','image2.png');
$pic2 = array('image4.gif','image5.jgp','image6.png');
$color = array('#FF0000','#00FF00','#0000FF',);

$number = rand(0,2);

$bgcolor = $color[$number];
$img1 = $pic1[$number];
$img2 = $pic2[$number];

?>

put this in the BODY-section:

<table bgcolor="<?php echo $bgcolor; ?>">
<tr><td>
<img src="your.web.site/yourfolder/<?php echo $img1; ?>">
<img src="your.web.site/yourfolder/<?php echo $img2: ?>">
</td></tr>
</table>

Post Reply