Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Quotation on every page using GridField


Go to End


3 Posts   1191 Views

Avatar
bones

Community Member, 110 Posts

7 March 2014 at 10:24am

Edited: 07/03/2014 10:24am

Hello
I'm finally moving up to SS3 from 2.4! My previous site used DOM for a list of quotations, and I then used the following code in page.php to display a random quotation on every page.

public function RandomQuotes() 
{ 
$limit = 1; 
return DataObject::get("Quote",null,"RAND()",null,$limit); 
}

I've got GridField working. But I can't work out how to modify the above code to give me the same result as previously.

All help appreciated. Thanks :)

Avatar
thomas.paulson

Community Member, 107 Posts

7 March 2014 at 5:40pm

public function RandomQuotes()
{
$limit = 1;
return Quote::get()->sort('RAND()') ->limit($num);
}

Avatar
bones

Community Member, 110 Posts

8 March 2014 at 1:35am

Thanks, Thomas. I've very grateful for your reply.

The code you supplied didn't work properly but you pointed me in the right direction. Here's the working version:

public function RandomQuotes() 
{ 
return Quote::get()->sort('RAND()') ->limit(1); 
}

Thanks again :D