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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Displaying random pages in homepage


Go to End


3 Posts   1841 Views

Avatar
anujkryadav

Community Member, 30 Posts

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 are

TouristplaceHolder.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

Avatar
Rishi

Community Member, 97 Posts

17 March 2010 at 5:37pm

Edited: 17/03/2010 5:38pm

Check out similar to this is already been solved i think

Avatar
nakashu

Community Member, 24 Posts

17 March 2010 at 9:36pm

Edited: 17/03/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/