Forum Moderators: coopster
I have added some adds to a side column, and I have added them all to a database. There are about 25 ads in rotation, I only want to show 5 at a time. i know I can use limit = 5 in my query, but can I add instructions so the five picked are random?
thanks!
I suggest to pick 5 random numbers within the script connecting to the database, and add these values to the query : "where id='RND1' or id='RND2' or id='RND3'".
if i do what he says, "where id='RND1 or id='RND2' etc, does that imply the following/impact my site:
1) I need to create an additional field in my table? (I think this defeats the purpose of random, so I am guessing the answer is no.)
2) In my code, I have created tables where each random # will appear (they are tables, one after the other)...can i simply echo the rnd# in each...ie use something like print rnd1
Thanks!
As barns101 said, you define 5 random numbers before querying the database.
There is another difference with the "simple solution", since in this case the random value is compare to you primary key (I suppose you have one), it has to be generated as an integer value lower than the max value (so you have to know that max value beforehand).
In php it looks like this: $rnd=rand(1,25);
dave1236: I don't understand you second question. Why do you need random numbers in you table, and why do you want to echo them?
i am drawing the ad code from my database, and there is probably a better way, but my first thought was the random feature.
The reason I am going down the random number path is because while the content may itself remain static, i want the ads to rotate - so that when a page reloads, the order/population of ads appearing will change.i am drawing the ad code from my database, and there is probably a better way, but my first thought was the random feature.
I understand that a simple method to get a random ad from the database is actually to use random(). What I don't understant from what you've said, is why do you add a random number to each record?
2) In my code, I have created tables where each random # will appear
And as Tynnhammar pointed out, the better method would be to ensure that each ad is equally displayed. random() is not always your best friend. ;)