Forum Moderators: coopster

Message Too Old, No Replies

'random' PHP queries

         

dave1236

9:04 pm on Dec 9, 2006 (gmt 0)

10+ Year Member



Is there a method to add a 'randomizing' feature to a query.

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!

dreamcatcher

12:36 am on Dec 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dave1236,

SELECT * FROM table ORDER BY rand() LIMIT 5

Should be fine.

dc

dave1236

1:31 am on Dec 10, 2006 (gmt 0)

10+ Year Member



Thanks!

Seems pretty easy... Certainly appreciate it!

Achernar

3:01 pm on Dec 10, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



Not very efficient.
A sequential scan will be done, a random() number will be calculated for every row in the table, then the rows will be sorted on these values, and it will take the first 5 rows.

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'".

FalseDawn

7:37 pm on Dec 10, 2006 (gmt 0)

10+ Year Member




Not very efficient.

There are only 25 records. Simplicity beats efficiency in this case.

dave1236

4:26 am on Dec 11, 2006 (gmt 0)

10+ Year Member



to get some further clarification re: Achernar's post...

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!

barns101

11:13 am on Dec 11, 2006 (gmt 0)

10+ Year Member



1) No, you would define 5 random number (e.g. using rand() [php.net]) before connecting to the database, and then just put the random number variables into your SQL query.

2) Yes, you'd probably use while [php.net] loop to display each table with the random data in it.

Achernar

12:52 pm on Dec 11, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



re dave1236 and barns101:

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?

dave1236

3:46 pm on Dec 11, 2006 (gmt 0)

10+ Year Member



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.

Tynnhammar

7:21 pm on Dec 11, 2006 (gmt 0)

10+ Year Member



Yea. Random is not the way to go, in my opinion, you should have some sort of altering system, so all ads are shown equally of how much they are worth.

Achernar

11:32 pm on Dec 16, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



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

There is no use of random numbers within the tables. In the query, yes.

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. ;)