Random image PHP code problem

I am attempting to pull a random image from a folder using PHP. I am very new to PHP and understand the basics, but had to look up code to do this. The following code is the simplest of what I found:

	<img src="commonImages/homepage/site_[php]<? php echo rand(1,4); ?>[/php].jpg" width="400px" height="400px"/>

I cannot get it to return the image. There are 4 images and they are all named “site_#.jpg.” I cannot figure out why I can’t get this to work. I have attempted to use longer php coding with named variables echoing back things like “+site_+$randnum+.jpg”, but none of it seems to work. Am I doing something wrong? Thanks in advance!

Well, try this instead:

[php]

[/php]

Now, to debug this, you can put it into a variable and display it. First, you should make sure that this is a valid address. How? Well, just go to your browser and type it in. Try typing this into your browser to test picture number 1:

www.yoursite.com/commonImages/homepage/site_1.jpg

It should display your first picture. If not, you have not layed out your URL pointing at the correct folder for your pictures. Depending on the rest of your code and where this routine is, you may have to add code to the front of it. So, if this routine is in your ROOT directory, they you will be all set. But, if it is in a folder called Scripts or whatever, then you would need to add “…/” to the beginning of the string.
You did not tell us the layout of your site or where this is being called from. But, you probably get the idea.

Hope that helps!

Sponsor our Newsletter | Privacy Policy | Terms of Service