Cakephp - 从数据库中随机选择并查看
我需要在我的帖子查看功能中创建三个指向其他帖子的随机链接。
控制器:
$random = $this->Post->find('all', array(
'order' => 'rand()',
'limit' => 3,
'conditions' => array('Post.status' => 'ok')
));
但我不知道如何为此编写foreach。
谢谢
I need create three random links to other posts, in my post view function.
Controller:
$random = $this->Post->find('all', array(
'order' => 'rand()',
'limit' => 3,
'conditions' => array('Post.status' => 'ok')
));
But i do not know, how to write a foreach for this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将取决于您从 Post 返回的字段。我会将控制器代码稍微更改为:
然后在视图中,在foreach中循环浏览它们:
请务必将HTML链接中的字段更新为那些符合 Post 模型返回的内容。
It will depend on the fields you get back from Post. I would change the controller code just slightly to this:
Then in the view you cycle through them in the foreach:
Be sure to update the fields in the HTML link to those that conform to what ever comes back from the Post model.
在我的本地计算机上,此代码可以正常工作,但在实时服务器上,它仅生成一次随机 id,之后重复相同的 id
On my local machine this code is working but on live server it generates random id just once, after that repeats the same id