Sum up numbers to a result

I need to sum up numbers from a table cell.

          <?php $items = $page->spenden()->toStructure();foreach ($items as $item): ?>
            <td><?= $item->datum() ?></td>
             <td><?= $item->plz() ?></td>
            <td><?= $item->betrag() ?>€</td>
              <?php endforeach ?>

It is the table cell “betrag” I need to sum at the end. I found $query->sum()in the docs and $query->result() but I don’t have an idea what to do with it and which is the right one.

How would I do it?

I doubt there is a direct Kirby function to sum up fields from within a structure, so I’d do it like this:

  $sum = 0;
  $items = $page->spenden()->toStructure();
  foreach ($items as $item):
    $sum = $sum + $item->betrag();
      <td><?= $item->datum() ?></td>
      <td><?= $item->plz() ?></td>
      <td><?= $item->betrag() ?>€</td>
  <?php endforeach ?>
<?= $sum; ?>

An alternative would be:

array_sum($page->spenden()->toStructure()->pluck('betrag', ','))
1 Like

A much more “Kirby” alternative, indeed! That would be my preference (and I was wrong with my assumption above - learning something new here every day).

You were not that wrong; it’s not a Kirby 3 function but a PHP function, see this.

Agreed; semi-wrong, then :smiley: …I once again was blissfully ignorant of the sheer endless utility of pluck(), which appears to be the answer to everything. A bit like 42.

1 Like


But how do I use it? Do I have to build a loop around it (didn’t work).

No, stupit, just go with <?= Its an echo, right?

<?= array_sum($page->spenden()->toStructure()->pluck('betrag', ',')) ?>

That’s it!

But how can I display this on the homepage? I tried:

<?php $items = page('spenden') ? page('spenden')->spenden()->toStructure() : array_sum($page->spenden()->betrag()->toStructure()->pluck(‘betrag’, ‘,’)) ?>

But I am like a monkey at a typewriter.

Or this inside the loop:

 <td><?= $item->betrag()->pluck(‘betrag’, ‘,’) ?>€</td>

`nother monkey.


What do you want to display on the homepage? Just the final sum in big letters?

I already display a table with numbers in it, which I pull from another page (see: Display content from page on homepage). Now I would like to display the sum in td as well.

Ok, then the $spendenPage variable should already be defined and we can continue to use that variable to get the rest of the info we need:

<?= array_sum($spendenPage->spenden()->toStructure()->pluck('betrag', ',')) ?>

Or if not, we make our check again:

<?= ($p = page('spenden')) ? array_sum($p->spenden()->toStructure()->pluck('betrag', ',')) : 'noch nix' ?>

Needed to check again.

When my confusion will have faded, I’ll need to examine this, to get it.

It’s basically the same as in the other example.

With the first part of the ternary operator

($p = page('spenden)) ?

we check if the page exists.

After the question mark comes the part that is executed if the condition is true.

It has two parts:


$p->spenden()->toStructure()->pluck('betrag', ',')

we create a flat array of all betrag entries, giving us an array like this:

    [0] => 2.50
    [1] => 2.80

The array_sum() method then adds up all elements in the array. Internally, it iterates through the elements and sums them up in a similar way as the solution provided by @sebastiangreger, but much faster.

Finally, after the colon comes the part that is rendered if page('spenden') returns nothing: we echo noch nix or an empty string.