RAND output 10 results?

I have a table where I store ads I have 20 of them here! I would like to show 10 ads at the site on the page. How can I randomly select and output 10 threads from a table that contains many of them!

If the are less than 10 threads then show all of them randomly rotating if more than 10 then randomly select 10.

If there’s less then 10 you could use shuffle() on the result set. If you’ve got more then 10 then select them all and then use array_rand() to pick 10 (or however many you need) at random from the result set.

1 Like

The problem I have is with this as it only shows one result:

$page = str_replace('{ads[zone_'.$zone['zid'].']}', $test, $page);

Do you mean something like this?


If there are many ads, then it’s quicker to have PHP do the shuffling. Speedy: ORDER BY RAND() operation - #10 by felgall

btw: @rpkamp do you still have the test data available from that thread to recreate the graphs? @ralphm do you know if all attachments from the vBulletin days still exist? If so the graphs could be extracted from the vBulletin version of the thread and added to the Discourse version of the thread. @Ophelie might be worth being the subject of an PHP article comparing the different methods of getting a certain number of random entries from a database table

No, sorry, I’m afraid I don’t.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.