我有一个配置文件表,我想以随机顺序列出配置文件,我已经完成了这样的操作:
$this->paginate = array('User' => array('conditions'=>array('User.userstanding_id'=>'1'), 'order' => 'RAND()', 'limit' => '10'));
唯一缺少的功能是,我希望配置文件按顺序排列,以便在下一页上有不重复,甚至当您浏览所有页面时,某些个人资料根本不会被查看。
我正在寻找一种方法来找到查询的随机起点,然后从该起点按顺序分页。也许每个会话?
I have a profiles table that I would like to have list profiles in Random order which I have accomplished like so:
$this->paginate = array('User' => array('conditions'=>array('User.userstanding_id'=>'1'), 'order' => 'RAND()', 'limit' => '10'));
The only functionality that is missing from this is that, I would like the profiles to follow sequentially so that on the next page there are not duplicates and even some profiles don't get viewed at all when you make it through all of the pages.
What I am looking for is a way to find a random starting point for the query then paginate in order from that starting point. Maybe per Session?
发布评论
评论(2)
MySQL
RAND()
函数 可以采用整数作为参数来用作种子,总是产生相同的结果:为了演示这一点,这里有一个包含 3 个单列字符串记录的表:
如果我使用 1 作为我的种子值
rand()
在我的ORDER BY
中,它总是会产生相同的结果排序:如果我将种子更改为 2,排序也会更改:
选择您的种子
您可以将用户的会话 ID 为一个整数并将其传递给此函数,以便对于该会话,结果始终以相同的方式排序。
例如,如果会话 ID 是 MD5 散列,为了避免处理这些值表示的大(160 位)整数,只需从字符串中删除字符(例如使用
preg_replace ('/[^\ d\s]/', '', $sessionid);
)。然后获取前 5 个左右的数字并将其转换为(int)
以用作种子:更新
使用 CakePHP 时将参数传递给
RAND()
函数,为确保将其视为整数而不是字符串,请尝试在$conditions
数组的order
中使用以下语法:The MySQL
RAND()
function can take an integer as an argument to use as a seed, always producing the same result:To demonstrate this, here's a table with 3 single-column string records in it:
If I use 1 as my seed value for the
rand()
in myORDER BY
it will always produce the same result ordering:If I change my seed to 2, the ordering changes:
Choosing your seed
You could convert the user's session ID to an integer and pass it in to this function so that for that session, the results would always be ordered in the same way.
If for example the session ID is an MD5 hash, to avoid having to deal with large (160 bit) integers that these values represent, just strip the characters from the string (using, for example
preg_replace ('/[^\d\s]/', '', $sessionid);
). Then take the first 5 or so digits and convert that to an(int)
to use as your seed:Update
To pass the parameter to the
RAND()
function when using CakePHP, to ensure it's treated as an integer rather than a string, try using the following syntax in your$conditions
array'sorder
:我不是 CakePHP 的专家,但我想如果您在会话的基础上播种 RAND 函数,那么分页函数将使用会话种子值逐页工作。
例如,
在创建会话时分配种子,只需使用随机数生成器来执行此操作。
I'm not an expert with CakePHP but I imagine if you seed the RAND function on a session basis then paginate function will work from page to page using the session seed value.
e.g
assign seed at creation of session, just use a random number generator to do this.