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.

Customising the CMS /

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

Display Content from Random child Page (based on Count)


Go to End


10 Posts   4958 Views

Avatar
LinseyM

Community Member, 99 Posts

16 July 2009 at 10:37am

Edited: 16/07/2009 10:39am

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

Avatar
LinseyM

Community Member, 99 Posts

4 August 2009 at 1:12am

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

Avatar
joshy

Community Member, 57 Posts

4 August 2009 at 10:14am

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 :)

Avatar
LinseyM

Community Member, 99 Posts

4 August 2009 at 8:40pm

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

Avatar
joshy

Community Member, 57 Posts

4 August 2009 at 8:46pm

Let me/us know how you get on :)

Avatar
LinseyM

Community Member, 99 Posts

12 September 2009 at 2:10am

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!!!

Avatar
LinseyM

Community Member, 99 Posts

12 September 2009 at 2:25am

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!

:)

Avatar
biapar

Forum Moderator, 435 Posts

22 October 2009 at 9:16am

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

Go to Top