基于session的随机查询结果

发布于 2024-11-02 03:50:43 字数 335 浏览 0 评论 0 原文

我有一个配置文件表,我想以随机顺序列出配置文件,我已经完成了这样的操作:

$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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南薇 2024-11-09 03:50:43

MySQL RAND() 函数 可以采用整数作为参数来用作种子,总是产生相同的结果:

为了演示这一点,这里有一个包含 3 个单列字符串记录的表:

mysql> select * from chartest;
+--------+
| string |
+--------+
| AA     |
| AB     |
| BB     |
+--------+
3 rows in set (0.00 sec)

如果我使用 1 作为我的种子值rand() 在我的 ORDER BY 中,它总是会产生相同的结果排序:

mysql> select * from chartest order by rand(1);
+--------+
| string |
+--------+
| BB     |
| AA     |
| AB     |
+--------+
3 rows in set (0.00 sec)

mysql> select * from chartest order by rand(1);
+--------+
| string |
+--------+
| BB     |
| AA     |
| AB     |
+--------+
3 rows in set (0.00 sec)

如果我将种子更改为 2,排序也会更改:

mysql> select * from chartest order by rand(2);
+--------+
| string |
+--------+
| AB     |
| BB     |
| AA     |
+--------+
3 rows in set (0.00 sec)

选择您的种子

您可以将用户的会话 ID 为一个整数并将其传递给此函数,以便对于该会话,结果始终以相同的方式排序。

例如,如果会话 ID 是 MD5 散列,为了避免处理这些值表示的大(160 位)整数,只需从字符串中删除字符(例如使用 preg_replace ('/[^\ d\s]/', '', $sessionid);)。然后获取前 5 个左右的数字并将其转换为 (int) 以用作种子:

$seed = substr($numeric_id, 0, 5);

更新

使用 CakePHP 时将参数传递给 RAND() 函数,为确保将其视为整数而不是字符串,请尝试在 $conditions 数组的 order 中使用以下语法:

'order' => 'RAND(CAST('.$seed.' AS SIGNED))' 

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:

mysql> select * from chartest;
+--------+
| string |
+--------+
| AA     |
| AB     |
| BB     |
+--------+
3 rows in set (0.00 sec)

If I use 1 as my seed value for the rand() in my ORDER BY it will always produce the same result ordering:

mysql> select * from chartest order by rand(1);
+--------+
| string |
+--------+
| BB     |
| AA     |
| AB     |
+--------+
3 rows in set (0.00 sec)

mysql> select * from chartest order by rand(1);
+--------+
| string |
+--------+
| BB     |
| AA     |
| AB     |
+--------+
3 rows in set (0.00 sec)

If I change my seed to 2, the ordering changes:

mysql> select * from chartest order by rand(2);
+--------+
| string |
+--------+
| AB     |
| BB     |
| AA     |
+--------+
3 rows in set (0.00 sec)

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:

$seed = substr($numeric_id, 0, 5);

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's order:

'order' => 'RAND(CAST('.$seed.' AS SIGNED))' 
孤千羽 2024-11-09 03:50:43

我不是 CakePHP 的专家,但我想如果您在会话的基础上播种 RAND 函数,那么分页函数将使用会话种子值逐页工作。

例如,

在创建会话时分配种子,只需使用随机数生成器来执行此操作。

 $this->paginate = array('User' => array('conditions'=>array('User.userstanding_id'=>'1'), 'order' => 'RAND('.$_SESSION['rndSeed'].')', 'limit' => '10'));

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.

 $this->paginate = array('User' => array('conditions'=>array('User.userstanding_id'=>'1'), 'order' => 'RAND('.$_SESSION['rndSeed'].')', 'limit' => '10'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文