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.

Link to comment
Share on other sites

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:

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.