21487 Posts in 5783 Topics by 2621 members
| Go to End | Next > | |
| Author | Topic: | 569 Views |
-
Get Random Image

19 October 2012 at 2:23pm Last edited: 31 October 2012 10:53am
I knew how to do this in SS2.X, but having problem in SS3. The background image is added in the CMS via SiteConfig.
I have BackgroundImage.php
class BackgroundImage extends DataObject {
public static $has_one = array(
'BackgroundImage' => 'Image',
'SiteConfig' => 'SiteConfig'
);
...
}I have SiteConfigCustom.php
class SiteConfigCustom extends DataExtension {
public static $has_many = array(
'BackgroundImages' => 'BackgroundImage'
);
...}
my old way:
Page.phpfunction RandomImage() {
return dataobject::get_one("BackgroundImage","SiteConfigCustomID",true,"Rand()");
}A couple I've tried (but the function makes the page break):
Page.phpfunction RandomImage() {
return BackgroundImage::get()->sort('RAND()');
and
return BackgroundImage::get()->filter('SiteConfigCustomID')->sort('RAND()');}
Any help much appreciated.
-
Re: Get Random Image

20 October 2012 at 8:53am
On my site I'm grabbing some random quotes from a class called Quote. In my Quotes class I have a checkbox for active quotes so I filter on that, sort by random and limit 1.
public function getQuote() {
$quotes = Quote::get()->filter(array('Active' => True))->sort('RAND()')->limit(1);
return $quotes;
}Also, in your example it looks like you are missing a single quote after "SiteConfigCustomID"?
return BackgroundImage::get()->filter('SiteConfigCustomID;)->sort('RAND()');
Hope that helps.
Chris
-
Re: Get Random Image

20 October 2012 at 9:19am
Hmmm...Still no joy. I get method 'fortemplate' does not exist on 'DataList'. That semi-colon wasn't actually there, just in the post.
I assume SiteConfig is making things harder than it should be.
-
Re: Get Random Image

25 October 2012 at 2:53pm Last edited: 25 October 2012 2:54pm
Have you tried...
BackgroundImage::get()->sort('RAND()')->First();
The key is the First() call at the end.
Sean
-
Re: Get Random Image

31 October 2012 at 11:13am
Sorry for the slow response, I just finished up a 5,000 mile road trip.
Looks like this works (if that's not the best way, let me know)...
Page.php
function SiteConfig() {
return BackgroundImage::get()->sort('RAND()')->First();
}Page.ss
<% loop SiteConfig %>
$BackgroundImage
<% end_loop %>Thank you!
-
Re: Get Random Image

31 October 2012 at 1:17pm
Ok, not quite working. It kills all of my other content being pulled from SiteConfig.
I tried changing it to the code below but that didn't seem to help.
function SiteConfig() {
parent::SiteConfig();
return BackgroundImage::get()->sort('RAND()')->First();
}Any suggestions?
-
Re: Get Random Image

1 November 2012 at 12:28am
change the name of your SiteConfig() function or even better move it to your SiteConfigCustom class. That's killing your other content from SiteConfig. Change it to something like getRandomImage(). So in your SiteConfigCustom class
public function getRandomImage(){
return $this->BackgroundImages()->sort('RAND()')->First();
}and then in your templates use
$SiteConfig.RandomImage()
or
<% loop $SiteCongig %>
$RandomImage
<% end_loop %> -
Re: Get Random Image

1 November 2012 at 5:52am Last edited: 1 November 2012 5:54am
Hi IOTI,
Still not working. Not showing an image at all.
Don't know if this is the proper way to extend the Controller on a custom SiteConfig.SiteConfigCustom.php
class SiteConfigCustom extends DataExtension {
…
}class SiteConfigCustom_Controller extends Page_Controller {
public function getRandomImage() {
return $this->BackgroundImages()->sort('RAND()')->First();
}
}Page.ss
<% loop SiteConfig %>
$RandomImage
<% end_loop %>
| 569 Views | ||
| Go to Top | Next > |




