Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Display Content from Random child Page (based on Count)

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

Page: 1 2
Go to End
Author Topic: 3067 Views
  • LinseyM
    Avatar
    Community Member
    99 Posts

    Display Content from Random child Page (based on Count) Link to this post

    Hi there,

    I really need help here before I go mad!

    On the left hand side of all my templates I want to display a small box with a random client comment.

    STEP 1)

    In the CMS I have set up a "Holder" page with the URL/ID "client-feedback-items". (This is a blank page)

    The children of this holder use a page type which has three custom fields:

    Quote (text)
    Name (text)
    Organisation (text)

    I am using the following naming convention for each new child page: clientFeedback1, clientFeedback2, clientFeedback3 and so on.

    The pages themselves are never displayed within the site structure (they are hidden from menu and search). Instead I want to display elements from them on most other pages (randomly picking one)

    I have an include, called by most pages in my site, named RandomQuote.ss

    This contains the following code at the moment:

    <div id="quote">
    <% control Page(clientFeedback1) %>
    <p class="words">"$Quote"</p><p class="person">$Name, <br/>$Organisation</p>
    <% end_control %>
    </div>

    This allows me to display the content of the first quote page.
    If I changed it to <% control Page(clientFeedback2) %> it would display the contents of that page instead.
    (This works so far...)

    STEP 2) this is where I am coming undone!!!

    I need to know how many child pages there are in the holder "client-feedback-items", then i need to pick a random number between 1 and the 'child count' of "client-feedback-items". I then have to return this value to the template page in order to display the random quote.

    Here's how I've tried to go about it:

    In the Page.php controller I added the following code: (idea was to count child pages then return a random number)

    function pickRandomQuote() {
          $upperLimit = $Children(client-feedback-items).Count; //count number of child items
          $random = rand(1, $upperLimit);
          $theRandomQuote = 'clientFeedback'.$random;
          return $this->$theRandomQuote;
    }

    then I changed the code in RandomQuote.ss as follows:

    <div id="quote">
    <% control Page($pickRandomQuote) %>
    <p class="words">"$Quote"</p><p class="person">$Name, <br/>$Organisation</p>
    <% end_control %>
    </div>

    (Replacing the hard-coded ID with the variable/funtion: $pickRandomQuote)

    For reason(s) that I don't understand it doesn't work. It's maybe something a bit obvious and stupid that I'm just not seeing (well, it's getting late now and I'm losing the will to go on!)

    I also tried using:
    $upperLimit = $Children.Count(client-feedback-items); //count number of child items

    and just a hard-coded one:
    $random = rand(1, 4);

    to test if it worked or if the count code was wrong, but it still didn't work.

    Any help would be much appreciated. I know I'm waffling on a bit, but its hard to explain it all!!!

    Thanks,

    L

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    Just bumping this up in the hope that someone can point me in the right direction...?

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    Heya,

    I *hope* this is what you're after (to go in the Page Controller and called by $RandomChild on the template):

    function RandomChild()
    {
       // This returns 1 object (last argument) of QuotePages (your group of quotes)
       // and the object returned is ordered randomly (RAND()) so it'll be random!
       $randomChild = DataObject::get("QuotePages", null, "RAND()", null, 1);
       return $randomChild;
    }

    In the template you should then do a control:

    <% control RandomChild %>$Quote by $Author<% end_control %>

    I must say that this would be much more efficient if you had a data-object of quotes rather than full pages. This tutorial goes into it really well:
    http://doc.silverstripe.org/doku.php?id=tutorial:5-dataobject-relationship-management

    I hope this helps

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    Thanks very much for your suggestions.

    I'll try it out and I'll also have a look at the alternative version too. Need to do a bit more reading on this: am just trying to learn on the fly while I develop my first couple of SS sites, so I am probably missing a lot of cool functionality!

    Cheers!

    L
    x

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    Let me/us know how you get on

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    Hi there,

    Sorry for the delay in getting back to you. Project was on hold for a wee while.

    I still can't get this to work. Keep getting errors and not sure how to fix them...

    Am having a look at the data object info just now but i think its going over my head!!!

  • LinseyM
    Avatar
    Community Member
    99 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    I take it back - I've just worked it out!!!

    In this part:
    $randomChild = DataObject::get("QuotePages", null, "RAND()", null, 1);

    I was setting the $obj (which you had called QuotePages) to the name of the parent page, whereas it should have been the PageType!

    DOH!

    Thank you!

  • biapar
    Avatar
    Forum Moderator
    435 Posts

    Re: Display Content from Random child Page (based on Count) Link to this post

    ... and if you want last 3...

    class HomePage_Controller extends Page_Controller {
       // Get random object
    function getRandomProductObject() {
    return DataObject::get('Product', null, 'RAND()',null,$limit=3);
    }

    }

    Bye

    3067 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.