Jump to content

PHP code for random image rotation?


healthbasics

Recommended Posts

I guess I screwed something up. 

 

Can I call it like this or no?

 

Do i have to cram that whole statement together as one into the spot where I want the image or did I just put a quote in the wrong place or something?

 

<?php 

$pic = array(
"shot1.jpg",
"shot2.jpg",
);

?>


</div>
	<div id="right-column">
		<div id="main-image"><img src="<?php echo "/img/'.$pic[rand(1,2)].'" ?>" height="240px" width="240px" alt="" /></div>
		<div class="sidebar">
			<ul id="menu">

 

My pics are stored in a subdir called img/ from the root.

 

mydomain.com/img/ for example.

 

Thanks.

halfway there.  This time it appears to work half the time.

 

Half the time it returns:

 

<div id="main-image"><img src="/img/" height="240px" width="240px" alt="" /></div>

 

and the other half

 

<div id="main-image"><img src="/img/shot2.jpg" height="240px" width="240px" alt="" /></div>

 

 

It seems like whenever it selects shot1.jpg from the random function it returns nothing....  :confused:

Not quite but following your logic I got it. 

 

what i needed was

 

<?php echo "/img/" . $pic[rand(0,1)]; ?>

 

<?php echo "/img/" . $pic[rand(0,2)]; ?> 

 

gave me each picture sometimes and once in a while a blank.  I figure 0,2 must be an array of 3 with values of 0, 1, 2...

 

Thanks!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.