Random image on refresh

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
brunko
Forum Newbie
Posts: 4
Joined: Wed Dec 09, 2009 10:31 am

Random image on refresh

Post by brunko »

Hi, everybody.
Got a problem with a specific thing. I got lost when trying to make simple header image rotating with 2 other images after refresh.
Want to have different image in header after refreshing a browser and should be linkable as well.
here is the code I need to modify:

$style_td = ' style="background-image: url(\''.$setting['shop']['url'].'img/blue.jpg\')"';

Thanx for help.
rufee
Forum Newbie
Posts: 10
Joined: Wed Dec 09, 2009 10:23 am

Re: Random image on refresh

Post by rufee »

I would use a random number for that

Code: Select all

$img = rand(1, 3);
$style_td = ' style="background-image: url(\''.$setting['shop']['url'].'img/$img.jpg\')"';
this will randomly display an image
images names have to be 1.jpg, 2.jpg and so on

if you want to display images in an order like 1, 2, 3 then use sessions
brunko
Forum Newbie
Posts: 4
Joined: Wed Dec 09, 2009 10:31 am

Re: Random image on refresh

Post by brunko »

I tried it but got
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING...
message...
rufee
Forum Newbie
Posts: 10
Joined: Wed Dec 09, 2009 10:23 am

Re: Random image on refresh

Post by rufee »

$img = rand(1, 3);
$style_td = ' style="background-image: url(\''.$setting['shop']['url'].'img/$img.jpg\')"';
oh the code tags displayed the code wrong
brunko
Forum Newbie
Posts: 4
Joined: Wed Dec 09, 2009 10:31 am

Re: Random image on refresh

Post by brunko »

I am neither :roll:
but ...I figured out that there is one php file called style.php and there is code for this pictures:

Code: Select all

 
.logotd{
 background: #fff url('img/3.jpg') no-repeat;
 width: 557px;
 height: 135px;
 vertical-align: top;
 }
where 3.jpg is one of the pictures wanted to be shown. Shouldn,t there be $img.jpg as well???
rufee
Forum Newbie
Posts: 10
Joined: Wed Dec 09, 2009 10:23 am

Re: Random image on refresh

Post by rufee »

this is a style for use in html <class="logotd"/>
its for something else in your page
EDIT: alright figured it out this should work
$img = rand(1, 3);
$style_td = ' style="background-image: url(\''.$setting['shop']['url'].'img/'.$img.'.jpg\')"';
brunko
Forum Newbie
Posts: 4
Joined: Wed Dec 09, 2009 10:31 am

Re: Random image on refresh

Post by brunko »

but when I want to change picture manualy I have to overwrite it with the same number I used in previous code....otherwise it wont change
Post Reply