Jump to content

Problem with paging random results


RonnyZenn

Recommended Posts

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
Share on other sites

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]
<?php
session_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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.