# Sum & count from grandchildren structure fields

**URL:** <https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595>\
**Category:** Questions\
**Tags:** v3\
**Created:** [October 4, 2021, 5:59pm UTC](https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595 "2021-10-04T17:59:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrfreedom](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mrfreedom/32/14018_2.png) [@mrfreedom](https://forum.getkirby.com/u/mrfreedom)\
**Post date:** [October 4, 2021, 5:59pm UTC](https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595/1 "2021-10-04T17:59:55Z")

</div>

Hi, I have one puzzle I just can’t solve, so any help is super appreciated.

Creating a Hippodrome racecourse events website, that has several levels of event structure.

– Events archive  
----- Seasons  
----------- Race days  
----------------- Individual races as structure fields

Now, I am trying to count all events (from the structure field), and display them as numbers on the Season page and Events archive page.

I did it on the Season page

```auto
<?php foreach ($page->children()->listed() as $event) :

    $races = $event->races()->toStructure();
    $gallop = $races->filterBy('raceType', 'gallop');
    $trot = $races->filterBy('raceType', 'trot');
    $hurdle = $races->filterBy('raceType', 'hurdle');
    $racesCount = $races->count();
    $gallopCount = $gallop->count();
    $trotCount = $trot->count();
    $hurdleCount = $hurdle->count();

    ?>

```

But, how to sum them from all Season pages on the Events archive page?  
Or count them from the Race days (grandchild)?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [October 4, 2021, 6:16pm UTC](https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595/2 "2021-10-04T18:16:32Z")

</div>

I‘d collect them all into a structure. There is an example somewhere here on the forum

> [@Merging structure fields](https://forum.getkirby.com/t/merging-structure-fields/12337/3):
>
> Try renaming /site/plugins/structure.php to /site/plugins/structure/index.php and put this inside: \<?php use Kirby\Cms\Structure; function createNewStructure($pages, $field) { // instantiate a new structure object $structure = new Structure(); // loop through the pages collection foreach ($pages as $p) { $structure-\>add($p-\>$field()-\>toStructure()); } return $structure; }

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [October 4, 2021, 7:38pm UTC](https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595/3 "2021-10-04T19:38:09Z")

</div>

sorry for not anwering your question but consider this: from my experience more complex data is usually better of in pages than in structures. pages can have models and this methods to provide precalculated data. using a pages section with a info containing html provides a nice preview and sorting is possible. i know editing content is not as fast as with structures but easier processing and the posibility of status (draft, unlisted, listed) counter that well.  
with a model even the draft status can be skipped and pages creates as unlisted/listed if thats needed.

---

<div class="post-metadata">

**Author:** ![mrfreedom](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mrfreedom/32/14018_2.png) [@mrfreedom](https://forum.getkirby.com/u/mrfreedom)\
**Post date:** [October 5, 2021, 10:51am UTC](https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595/4 "2021-10-05T10:51:23Z")

</div>

I really don’t know which is better, Kirby itself or your support on the forum. 🙏

Collecting in the structure was the most straightforward solution, and it works great. 🕶

Here is the solution, if someone needs it in the future:

Created plugin as instructed in [this thread](https://forum.getkirby.com/t/merging-structure-fields/12337/2), and created foreach loop with additional structure collection

```auto
<?php foreach ($page->children()->published()->sortBy('title', 'desc') as $season) :

        // fetch a collection of pages https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595
        $eventCollection = $season->children()->listed();

        // pass the collection of pages and the name of the field to the function https://k2.getkirby.com/docs/cookbook/the-structure-field#merging-structure-fields
        $entries = createNewStructure($eventCollection, 'races');

        // filter races
        $gallopRaces = $entries->filterBy('raceType', 'gallop');
        $trotRaces = $entries->filterBy('raceType', 'trot');
        $hurdleRaces = $entries->filterBy('raceType', 'hurdle');

        // count races
        $totalRaces = $entries->count();
        $totalGallop = $gallopRaces->count();
        $totalTrot = $trotRaces->count();
        $totalHurdle = $hurdleRaces->count();

    ?>

<?= $totalRaces ?>
<?= $totalGallop ?>
...

<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![mrfreedom](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/mrfreedom/32/14018_2.png) [@mrfreedom](https://forum.getkirby.com/u/mrfreedom)\
**Post date:** [October 5, 2021, 10:59am UTC](https://forum.getkirby.com/t/sum-count-from-grandchildren-structure-fields/23595/5 "2021-10-05T10:59:18Z")

</div>

This is something I am super interested in, but I still can’t fully understand the logic of page models, and possibilities. If you or anyone else don’t know the subject for some new Kirby cookbook article page models would be a great thing. I would even pay for articles or tutorials on that or similar advanced subjects.
