Cakephp 分页随机顺序?
好吧,我看了又看,但似乎在任何地方都找不到任何相关内容。我显示的结果分页精美,但它们目前按升序显示。我希望它们以随机顺序显示。这是我当前的控制器代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于其他发现此问题的人 - 实际答案是生成一个种子(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'sbeforeFilter()
). 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.对我来说,这似乎在 CakePHP 2 上运行得很好:
这是使用 MySQL
RAND()
函数,Cake 只是将其传递到数据库。编辑:现在我想了一下,这不是一个好主意,因为页面之间的顺序不保持。我想不出一个好的方法来随机化顺序并保持页面之间的连续性。也许您要使用 JavaScript 来随机排列页面上的项目?
This seems to work pretty well on CakePHP 2 for me:
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?