Cakephp 分页随机顺序?

发布于 2024-12-26 04:24:21 字数 718 浏览 1 评论 0原文

好吧,我看了又看,但似乎在任何地方都找不到任何相关内容。我显示的结果分页精美,但它们目前按升序显示。我希望它们以随机顺序显示。这是我当前的控制器代码:

public function condos() {          

$this->paginate['Unit']=array(
        'limit'=>9,
        'contain'=>array(
                'User'=>array(
                    'id', 'user_name', 'area_code', 'exchange', 'sln', 'email'),
                'Complex'=>array('id','complex_name', 'realname', 'photo1','photo2','photo3','photo4','photo5', 'complex_website')
                    ),
        'conditions'=>array(
                'Unit.type'=>array('condo', 'rentalco'),
                'Unit.active'=>1)   
    );
$data = $this->paginate('Unit');
$this->set('allcondos', $data);


}

Ok, I have looked and looked but cannot seem to find anything on this anywhere. I have a display of results that are paginated beautifully, but they currently display in ascending order. I'd like for them to display in random order. Here is my current controller code:

public function condos() {          

$this->paginate['Unit']=array(
        'limit'=>9,
        'contain'=>array(
                'User'=>array(
                    'id', 'user_name', 'area_code', 'exchange', 'sln', 'email'),
                'Complex'=>array('id','complex_name', 'realname', 'photo1','photo2','photo3','photo4','photo5', 'complex_website')
                    ),
        'conditions'=>array(
                'Unit.type'=>array('condo', 'rentalco'),
                'Unit.active'=>1)   
    );
$data = $this->paginate('Unit');
$this->set('allcondos', $data);


}

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

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

发布评论

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

评论(2

寄风 2025-01-02 04:24:21

对于其他发现此问题的人 - 实际答案是生成一个种子(0 和 1 之间的浮点数),并在需要 RAND() 排序之前将其保存到会话中(在控制器的 beforeFilter())。然后:

$this->paginate['order'] = sprintf('RAND(%f), $this->Session->read('seed'));

这保留了RAND() 在分页器调用之间播种,并保留请求之间结果的总体顺序。

For anyone else finding this - the actual answer is to generate a seed (a float between 0 and 1), and save it to the session before the RAND() sort is necessary (in the controller's beforeFilter()). Then:

$this->paginate['order'] = sprintf('RAND(%f), $this->Session->read('seed'));

This preserves the RAND() seed between calls to the paginator, and preserves the overall order of the results between requests.

乖乖 2025-01-02 04:24:21

对我来说,这似乎在 CakePHP 2 上运行得很好:

$this->paginate = array(
    'order' => 'RAND()'
);

这是使用 MySQL RAND() 函数,Cake 只是将其传递到数据库。

编辑:现在我想了一下,这不是一个好主意,因为页面之间的顺序不保持。我想不出一个好的方法来随机化顺序并保持页面之间的连续性。也许您要使用 JavaScript 来随机排列页面上的项目?

This seems to work pretty well on CakePHP 2 for me:

$this->paginate = array(
    'order' => 'RAND()'
);

This is using the MySQL RAND() function, which Cake just passes on to the database.

EDIT: Now that I think about it, this is not a good idea, because the order is not maintained between pages. I can't think of a good way off the top of my head to randomize the order and maintain continuity between pages. Maybe if you were to shuffle the items on the page with JavaScript?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文