Random sort issue (page shuffle

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lubber123
Forum Commoner
Posts: 51
Joined: Mon Sep 15, 2008 12:26 pm
Location: Manassas, VA

Random sort issue (page shuffle

Post by lubber123 »

I have a logic problem concerning running a random search between two numbers to shuffle items on my web page. I do a query to my db and receive the row id's for certain data. What i want to do is find randomly place the items ion my page each time a user comes to my page so that it looks fresh each time. The logic has to find each row randomly and place it on the page only once.

So let's say I retrieve the id's 2, 4, 5, 6, and 9. I want to display its data randomly each time. I thought about placing the id's in an array and then running the random function between the lowest and highest numbers - marking each off when they are discovered. But I think that could take a long time if the number of retrieved id were to grow - finding each number randomly would take a while for the page to load.

Does anyone understand what i am trying to do here and can anyone help?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Random sort issue (page shuffle

Post by VladSun »

Do it in DB layer - use ORDER BY RAND() and LIMIT
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply