Forum Moderators: coopster

Message Too Old, No Replies

php for random backgrounds

         

Kysmiley

3:40 am on Feb 5, 2005 (gmt 0)

10+ Year Member



Can PHP be used to display a different background on a page when a vistor visits. I have a set up pages that I would likme to beable to show a different background that is randomly selected from an array of background images. Can anyone point me in the correct direction to find or learn how to make this possible if it can be done in PHP
Pat

QuazBotch

4:12 am on Feb 5, 2005 (gmt 0)

10+ Year Member



Name your backround image files 1.jpg, 2.jpg, 3.jpg 4.jpg and so on. Use PHP to generate a random number and save it as a variable ($bg sounds good) from 1 to 4 (or however many backgrounds there are).

In your css or html or wherever you specify the background for the page, do something like

background-image:url(<?php echo $bg?>.jpg);

So the browser will load whatever background corresponds to the random number PHP made up.

dreamcatcher

9:00 am on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use numbers, or the file extensions. Just store the file extensions in an array, then give the array a shuffle, then pull data from one random slot:

$ext = array("pic1.jpg", "pic2.jpg", "pic3.jpg"); //etc

shuffle($ext); //Give the array a shuffle

background-image:url('images/<?php echo $ext[0];?>');

This would display the data in the first slot. File extensions might be better as you could loop through your images directory and assign the extensions to an array there, then add new ones if you wish.

Hope that helps.

Kysmiley

10:05 am on Feb 5, 2005 (gmt 0)

10+ Year Member



Thank you very much you two. I will work with that code and see if I can get it to work
I did not know if the shuffel would wo0rk or not
pat

dreamcatcher

12:18 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. Let us know how you go.

dc

Kysmiley

12:21 am on Feb 6, 2005 (gmt 0)

10+ Year Member



Not working as it should I am getting an error message this is what i'm using
#################
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<? php
$ext = array('acbg1.jpg', 'acbg2.jpg', 'acbg2.jpg','acbg4.jpg','acbg5.jpg','acbg6.jpg',); //backgrounds
shuffle($ext); //Give the array a shuffle
?>
<html><head>

<style type="text/css" media="all">
body {color: #000; background: transparent; background-image: url('./dir/<?php echo $ext[0];?>');
}

</style>
#########################
Error mesage is
#########################
Parse error: parse error, unexpected T_VARIABLE in /dir/folder/page.php on line 4
##################
Can Anyone tell me what im doing wrong to get the random backgrounds showing on the pages. I have even read my php book and this is also the same coding it shows or close to it
pat

jamesa

3:22 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<? php in line 3 should be <?php (no space). Don't know if it was just a typo in your post, but I got the same error when the space was there.

Kysmiley

3:30 am on Feb 6, 2005 (gmt 0)

10+ Year Member



Thanks Jamesa that was part of it and I had a comma at the end of the array. I fixed bot now my wife loves the result on her site
Pat

dreamcatcher

9:41 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool. Glad the result was successful.

dc

Kysmiley

1:50 pm on Feb 6, 2005 (gmt 0)

10+ Year Member



Its easy dreamcatcher when I have great people like yourself to help mde out so I can learn while Im getting what I need. The best part is its free LOL
Pat