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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

displaying random child in homepage


Go to End
Reply


9 Posts   2054 Views

Avatar
Rishi

Community Member, 97 Posts

5 March 2010 at 10:29pm

I have a holder page and its child page where i am inserting text and images.
Tt the moment i am displaying only the images from child page in the holder page.
this much is working fine.

Things need to be done
I want to randomly display image and first line of the text inserted in three child pages in my homepage
my code are
Holder page name is Horses.php

<?php
class Horses extends Page {
static $db = array(
);
static $has_one = array(
);

static $allowed_children = array('HorsesPage');
}

class Horses_Controller extends Page_Controller {

public function GetHorsesPage(){
return DataObject::get("HorsesPage");
}
}

?>

Child page name is HorsesPage.php

<?php
class HorsesPage 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
);
static $allowed_children = array('none');
}

class HorsesPage_Controller extends Page_Controller {

}
?>

Example Code will be of great help i am new for silverstripe.
Thank you in advance

Avatar
UncleCheese

Forum Moderator, 4096 Posts

6 March 2010 at 4:00am

You're writing a little too much code.. The "GetHorsesPages" function is useless because what you're really interested in is the Children pages. GetHorsesPages is too inclusive and has the possibility of returning orphaned HorsePages.

In your Horse_Controller, add this:

public function RandomChildren($max = 3)
{
return DataObject::get("HorsePage","ParentID = $this->ID","RAND()",null,"0,$max");
}

<% control RandomChildren %>
Random title: $Title
<% end_control %>

Avatar
Rishi

Community Member, 97 Posts

6 March 2010 at 7:35am

Thank you UC
i tried the code and it is giving me an error

[User Error] Bad class to singleton() - HorsePage
showing error in this line
if(!class_exists($className)) user_error("Bad class to singleton() - $className", E_USER_ERROR);

Avatar
UncleCheese

Forum Moderator, 4096 Posts

6 March 2010 at 8:02am

HorsesPage... whatever you called it.

Avatar
Rishi

Community Member, 97 Posts

11 March 2010 at 6:43am

Edited: 11/03/2010 7:00am

UC sorry for my mistake,your code works great,
another silly question hope you dont mind
i want to display the images uploaded in the child page.though i have displayed them in the parent page but i am not been able to set the height and width,is it possible to set the and width of images dispalyed.
my template page contain this code

<% control RandomChildren %>
$Title
$Photo

<% end_control %>

Another thing i want to do is calling 3 random pages in my homepage i tried with this code but it is giving me error,i think i ned to write the page parent page but dont no how and where
public function RandomChildren($max = 3)
{
return DataObject::get("HorsePage","ParentID = $this->ID","RAND()",null,"0,$max");
}
thanks for helping me so much

Avatar
UncleCheese

Forum Moderator, 4096 Posts

11 March 2010 at 8:34am

What do you mean it gave you an error? What error?

There are a bunch of things you can do for image resampling. You should really read the docs..

$Photo.SetWidth(100)

$Photo.CroppedImage(50,50)

<% control Photo %><img src="$URL" class="class" / ><% end_control %>

etc...

Avatar
Rishi

Community Member, 97 Posts

12 March 2010 at 8:15am

thank you UC that works great for me the image size is being adjusted nicely.
the code which you have posted works great for a child pages
but i am trying to display random child page on my home page.let me b clear using the site tree

-HOME
-Universities
--University1
--university2
--university3
--university4
--university5
-Student
contact us

Now i was trying to display 3 random university pages on my home pages and i tried the following code

public function RandomChildren($max = 3)
{
return DataObject::get("HorsePage","ParentID = $this->ID","RAND()",null,"0,$max");
}

university1 university 2 etc have page type as HorsePage.

regarding error -i am sorry i was not been able to make you understand
error means nothing is showing on homepage
if i make child of homepage with page type as horsepage then only it get displayed .
but i dont want that i want to get random university on my homepage

thank you in advance.
and thanks for the suggestion about the documents.i am going through the documents which i am able to find,i got a link about the test which is
http://doc.silverstripe.org/doku.php?id=text
but i have not been able to find any document about pictures.it will b really great of you if you can guide me on finding the document s of silverstripe regarding the programming
thank you again you are really a great help.without you i would have been bangining my head on wall

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 March 2010 at 8:35am

If the page "Universities" is the only one of its kind, you can just get that and return random children.. or you can control it by URLSegment

<% control Page(Universities) %>
<% control RandomChildren %>

<% end_control %>
<% end_control %>

Or, just write a function in your Page_Controller that will be accessible by all your pages.

public function ChildrenOfUniversities($num = 3)
{
if($page = DataObject::get_one("Horse")) return $horse->RandomChildren($num);
return false;
}

Go to Top