construct an image url based on variables.

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
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

construct an image url based on variables.

Post by markosjal »

I need to load a gif image based on a couple of variables

First I am trying to use a $catid that I have successfully echoed to the page from an existing php script.

Secondly the image itself is randomly selected for loading. In the example below, this should load any one of two images as I understand it, and the directory to be determined by the $catid variable

Code: Select all

<img src="/banners/<? echo $catid; ?>/banner<? echo rand(0, 1); ?>.gif"> 
Should this not work? Any pointers?

thanks,

Mark
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: construct an image url based on variables.

Post by mrcoffee »

There is no built-in function named "random".

PHP has a few random number generators to choose from, but "mt_rand(0,1)" would work fine assuming you have images named banner0.gif and banner1.gif.

Hope that helps.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: construct an image url based on variables.

Post by markosjal »

I must have edited this while you were posting.

I corrected "random" to "rand" in this post.
mrcoffee
Forum Commoner
Posts: 31
Joined: Tue Nov 10, 2009 3:03 pm
Location: Wyoming, USA

Re: construct an image url based on variables.

Post by mrcoffee »

Oh, well then, yes that should work.

What is not working about it? What shows up for the image source when you load the page in a browser?
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: construct an image url based on variables.

Post by markosjal »

Actually, I have now ended up going a different route with this.

Thanks anyway. I created a function to create a variable so that I could easily connect it with a URL variable as well.
Post Reply