7607 Posts in 1236 Topics by 846 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Displaying random pages in homepage
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 816 Views |
-
Displaying random pages in homepage

14 March 2010 at 8:10pm
hello
i have created a page holder and a child page type,i am displaying random child in the holder page and it is working fine( got help from one of the thread) but now i need to display random 3 child in my homepage,help me out in doing that ,my code for the pages areTouristplaceHolder.php
<?php
class TouristplaceHolder extends Page {
static $db = array(
);
static $has_one = array(
);}
class TouristplaceHolder_Controller extends Page_Controller {
public function RandomChildren($max = 3)
{
return DataObject::get("TouristPlace","ParentID = $this->ID","RAND()",null,"0,$max");
}
}?>
TouristPlace.php
<?php
class TouristPlace extends Page {
static $db = array(
);
static $has_one = array(
'Photo' => 'Image'
);function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Images", new ImageField('Photo'));
return $fields;
}static $defaults = array(
'ShowInMenus' => false
);}
class TouristPlace_Controller extends Page_Controller {
}
?>TouristplaceHolder.ss
<% control RandomChildren %>
Random title: $Title
Photo:$Photo
<% end_control %>the above code is working fine for me .what i need to do is to display random 3 TouristPlace pages in my home page
thank you in advance
-
Re: Displaying random pages in homepage

17 March 2010 at 5:37pm Last edited: 17 March 2010 5:38pm
Check out similar to this is already been solved i think
-
Re: Displaying random pages in homepage

17 March 2010 at 9:36pm Last edited: 17 March 2010 9:37pm
I main think to the code to work also for your homepage is adding the the random function into page controller or homepage controller.
public function RandomChildren($max = 3)
{
return DataObject::get("TouristPlace","ParentID = $this->ID","RAND()",null,"0,$max");
}should work this way.
It because when you have the function inside the TouristplaceHolder.php it is accessible only from that pagetype and pages extending it..
But if you'll add it inside Homepage.php it will accessible also from that pagetype and pages extending it.And as most pages extend your page.php and page_contoller.. if you add it inside page_controller .. any page that extends page will have access to that function and you would be able to use it site wide.
hope it solved the problem. . take care..
p.s.: some info on the topic can be extracted from here:
http://www.ssbits.com/create-a-static-sidebar/
and these two you know probably:
http://www.ssbits.com/getting-objects-from-multiple-child-pages/
http://www.ssbits.com/displaying-fields-from-a-set-of-pages-on-another-page/
| 816 Views | ||
|
Page:
1
|
Go to Top |


