erme Posted August 13, 2009 Share Posted August 13, 2009 Hi, I'm building a business directory. When a user searches for a business I want the results to display randomly, as it would be unfair to order by date or alphabetically. This is simple enough. Except I need to be able to save the random selection for pagination. This way there is no repeat of records and the user can view all the records available. Is this possible? And if so, can anyone help with some strategies? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170172-selecting-random-records-and-holding-for-pagination/ Share on other sites More sharing options...
benphelps Posted August 14, 2009 Share Posted August 14, 2009 I'm 100% sure there is a better way to do this, I just don't know how. $array = shuffle(mysql_fetch_array(mysql_query("SELECT * FROM table"))); $save = serialize($array); $open = unserialize($save); You could save the array in a database and tag it to the session for the user, that way the user has the same, but random, results. You can treat the array you get from $open just like you would if it was never changed. The only thing you loose the the "LIMIT" from the SQL, which is how most pagination scripts work if I'm not mistaken. Quote Link to comment https://forums.phpfreaks.com/topic/170172-selecting-random-records-and-holding-for-pagination/#findComment-897802 Share on other sites More sharing options...
sasa Posted August 14, 2009 Share Posted August 14, 2009 you can use mysql function RAND() with seed in ORDER BY part Quote Link to comment https://forums.phpfreaks.com/topic/170172-selecting-random-records-and-holding-for-pagination/#findComment-897843 Share on other sites More sharing options...
roopurt18 Posted August 14, 2009 Share Posted August 14, 2009 If there are many rows in the database, then benphelps approach will be inefficient and essentially defeat the purpose of pagination. Also, his syntax is wrong. I believe sasa is saying to use the same seed every time you run the query, which will produce the same random values. You just have to track it in the user's session then and that may work. I don't know if MySQL supports them, but you may be able to accomplish this with a cursor. Quote Link to comment https://forums.phpfreaks.com/topic/170172-selecting-random-records-and-holding-for-pagination/#findComment-897858 Share on other sites More sharing options...
erme Posted August 14, 2009 Author Share Posted August 14, 2009 Hmm, could be more hassle then its worth Quote Link to comment https://forums.phpfreaks.com/topic/170172-selecting-random-records-and-holding-for-pagination/#findComment-898029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.