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.

General Questions /

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

Count within Children control?


Reply


7 Posts   724 Views

Avatar
johnofjack

Community Member, 22 Posts

31 December 2011 at 3:23am

Edited: 31/12/2011 3:44am

Is there a way to count the number of children of each type within the children control? $TotalItems just gives the number of types of children, which is not what I'm looking for.

An example would be if you had links to genres of movies in a Children control showing comedies, action films, and westerns. $TotalItems would print 3, but I don't want to know how many genres of movies there are in the database; I want to know how many there are of each one e.g. comedy (5), action (9), western (3).

Avatar
danzzz

Community Member, 175 Posts

31 December 2011 at 4:43am

hi

can you tell us more about your code? do you just use the normal page type? do you added fields?

you can use different pagetypes for different genres, then $TotalItems should work fine.

an other way is to wirte an own function where you manualy get your dataobject and then render a special template in a loop

Avatar
johnofjack

Community Member, 22 Posts

31 December 2011 at 7:00am

Ah, well the genres were just an example. What I've got is this:

A pagetype ActionPage, with pages like "removal from library," "restriction of access," etc. These are held in ActionHolder.

ActionHolder uses % control Children % to list the titles of each page and link to them. Beside the title I'd like a count of how many of each type there are (e.g. removal from library (6), restriction of access (11)).

$Title and $Link are working as expected (they change each time), but after them $TotalItems always gives (14), which isn't what I wanted. $Count within % control Children % is always blank.

Does that make sense? I feel like this probably has a simple answer and I'm just not seeing it.

Avatar
danzzz

Community Member, 175 Posts

31 December 2011 at 8:33am

hi

I understand ...

try

$Title ($Children.count)

Avatar
johnofjack

Community Member, 22 Posts

31 December 2011 at 9:00am

That returns 0 for everything.

I realized why this isn't working, and it's not working because I'd forgotten how it's all set up on the backend.

On any given ArticlePage you can check a box for Actions (corresponding to ActionPages), so I'm actually trying to do two different things at once: on ActionHolder, list and link to the Children. Also, count how many times each of those Children appear in an entirely different table.

Could I maybe write a function to count, with $this->ID as a variable to be put into the SQL, and use that function within Children? Or is it more complicated than that?

Avatar
danzzz

Community Member, 175 Posts

31 December 2011 at 9:15am

are your pages organized this way:

ActionHolder
-->ActionPage
---->Page

?

I understand it like that ... in this case $Children.count should work on the second level (ActionPage).

can you post code of ActionPage.php?

You have some Checkbox fields there and want the count of the checkd boxes right?

If so ...

To count the active checkboxes is possible with a function but not the best way to solve this, because when you add some new checkboxes you also have to modify the code again.

But post Holder ActionPage code please.

Avatar
johnofjack

Community Member, 22 Posts

31 December 2011 at 9:43am

No, they're arranged like this:
ActionHolder
-->ActionPage
ArticleHolder
-->ArticlePage

with a many_many between them.

I figured it out, though. I wrote a function to count what I needed, and it worked once I moved it out of ActionHolder.php into ActionPage.php (since I was trying to call it in the control Children on the page ActionHolder).

Thanks for your help.