Jump to content

Getting A Random Integer


dzhax

Recommended Posts

I am trying to make my own advertisement rotator. I am using a switch statement to list all of the advert image sources, but i need the script to generate a random integer (obviously within the number of cases) for it to select another image to display. Then from there according to the image displayed, select the correct link that corresponds. The reason I am making my own is 1. I can not get Web Expressions ASP.NET Ad Rotator to work. 2. I do not want to make is .aspx. I would like it to stay .php\Thanks for your help guys I know you will help lead me in the right way.----Forgot the code lol.This is what I have so far, then later down I am print"$advert"; in the image source.

<?phpswitch($rndNum){case 1:  $advertImg = "picture source...";  break;}?>

Link to comment
Share on other sites

mt_rand() is much faster and more random than rand().The answer of your question is right in the PHP manual.If you want 6 cases and for the max value to be 6, then the minimum value must be 1. Therefore, you would write mt_rand(1,6);
Link to comment
Share on other sites

i understand that part but the 6 has to be dynamic. I have a bad habit of forgetting to update variables like that.Like it check to see how many cases are written and set that number to the max.I got the rest to work I would just like the dynamic max.

Link to comment
Share on other sites

as of a little bit ago it was working with the static max number. Im gonna leave it as it is for now. I dont wanna mess with arrays. I might break it more then it already is. I tried to add more stuff to it and broke again lol. I made another post if you wanna check it out.

Link to comment
Share on other sites

Arrays are a very good way of organizing things in your code.Here's an example of a really simple ad rotator. Each time the page reloads, a random ad will appear

$ads = array(  "<a href='http://adsite1.com'><img src='/images/ad1.gif' alt='Company name 1'></a>",  "<a href='http://adsite2.com'><img src='/images/ad2.gif' alt='Company name 2'></a>",  "<a href='http://adsite3.com'><img src='/images/ad3.gif' alt='Company name 3'></a>");echo array_rand($ads);

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...