Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Randomly load ChildrenWithLimit

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 522 Views
  • mhull
    Avatar
    Community Member
    79 Posts

    Randomly load ChildrenWithLimit Link to this post

    I am using the following code to pull 2 children of the StaffHolderpage into the Homepage.

    Page.php:

    function ChildrenWithLimit($limit = 2) {
    return DataObject::get('Page', "ParentID = $this->ID", '', '', $limit);
    }

    Homepage.ss:

    <% control Page(about-us) %>

    <ul id="StaffList">
    <% control ChildrenWithLimit %>

    <li>
    <div class="staffname"><a href="$Link">$Title</a></div>
    <div class="staffphoto">$Photo</div>
    <div class="staffdescription"><p>$Content.FirstSentence</p></div>
    </li>
    <% end_control %>
    <% end_control %>


    But what I hope to do, is to randomly choose 2 of these. Is this possible? how would it be done? Any help would be much appreciated.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Randomly load ChildrenWithLimit Link to this post

    Hi
    Using "RAND()" for the sorting should do the trick. Eg.

    return DataObject::get('Page', "ParentID = $this->ID", 'RAND()', '', $limit);

  • mhull
    Avatar
    Community Member
    79 Posts

    Re: Randomly load ChildrenWithLimit Link to this post

    Many Thanks for your help, this all seems to be working!

    522 Views
Page: 1
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.