RonnyZenn Posted January 16, 2007 Share Posted January 16, 2007 hi,I wrote a script to search the database and page the results.There is no problem upto here. But I want to display in different order each time. I tried to use ORDER BY rand() and it seems working but there are two problems with that,First, as I cannot follow the order,sometimes same result(s) is diplayed in more than one page.And the second problem is, when i click on the list to any link and then go back to list, the result list is changed as everytime ORDER BY rand() runs.As far as I see ORDER BY rand() is not what i am looking for.I thought I could insert SQL results into array and then page the array to follow the order but again how am I gonna pass the array to next pages to follow the order?!I am a bit mixed-up here. Could be great if someone could help me.Cheers Link to comment https://forums.phpfreaks.com/topic/34386-problem-with-paging-random-results/ Share on other sites More sharing options...
utexas_pjm Posted January 16, 2007 Share Posted January 16, 2007 This post assumes you're using MySQL. The RAND() function takes as an optional argument an integer N which is used to "seed" the random float it returns. This allows you to return predictable results when passing a constant N to RAND(). So what I would do is use a substring of the user's session_id to seed the RAND function as follows:[code]<?phpsession_start();$seed = hexdec(substr(session_id(), 0, 2));$sql = 'SELECT `foo`, `bar` FROM `table` LIMIT 10 OFFSET 0 ORDER BY RAND('.$seed.')';// ... etc?>[/code] Hope this helps.Best,Patrick Link to comment https://forums.phpfreaks.com/topic/34386-problem-with-paging-random-results/#findComment-161936 Share on other sites More sharing options...
utexas_pjm Posted January 16, 2007 Share Posted January 16, 2007 The SQL statement should read:[code]SELECT `foo`, `bar` FROM `table` ORDER BY RAND('.$seed.') LIMIT 10 OFFSET 0[/code]What happened to the moodify post option? Link to comment https://forums.phpfreaks.com/topic/34386-problem-with-paging-random-results/#findComment-161937 Share on other sites More sharing options...
RonnyZenn Posted January 19, 2007 Author Share Posted January 19, 2007 utexas_pjm, thank you for you answer.This way might be usefull also but as i was in a kind of hurry, i found and used someother technic. I would like to explain it here for other people who may be intereste in future.What I did is, First I search with RAND() then put all the results into a SESSION, cookie could be used also, then I used session just like array and made pagination thru it.Thank you again. Link to comment https://forums.phpfreaks.com/topic/34386-problem-with-paging-random-results/#findComment-164393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.