Page 1 of 1

construct an image url based on variables.

Posted: Fri Apr 16, 2010 10:46 pm
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

Re: construct an image url based on variables.

Posted: Fri Apr 16, 2010 11:10 pm
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.

Re: construct an image url based on variables.

Posted: Sat Apr 17, 2010 12:21 am
by markosjal
I must have edited this while you were posting.

I corrected "random" to "rand" in this post.

Re: construct an image url based on variables.

Posted: Sat Apr 17, 2010 1:23 am
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?

Re: construct an image url based on variables.

Posted: Sat Apr 17, 2010 2:38 am
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.