2011-03-11 59 views

回答

7
$this->Quote->find('first', array('order' => array('rand()'))) 
2

$this->Testimonial->findAll(null,null,'rand()',1,null,null); 

不幸的findAll不存在了,那么你可以试试这个:我把这个网址

$count = $this->Testimonial->find('count'); 
$this->Testimonial->find('first', array('conditions' => array('id' => rand(1,$count)))); 

(这也不会检索 “全”结果)

+1

如果我的ID不是连续的顺序会怎样? – 2011-03-11 15:47:40

+0

然后你只是检索所有的结果,然后随机选择一个, '$ find = $ this-> Model-> find('all'); echo $ find [rand(0,count($ find))] ['Model'] ['someName']' – Thomas 2011-03-29 14:56:30

+1

@Thomas:这会浪费很多资源,因为所有数据都必须从数据库PHP只是为了扔掉。最好让数据库服务器处理挑选正确的数据集。 – 2011-08-17 13:26:54

相关问题