Adds a sum($selector, $field) method to the $pages class. Use for very fast summations across large data sets.

PagesSum Module For ProcessWire

This is a (very) slightly rewritten version of ESRCH's original PagesSum module for Processwire CMS/CMF. You can find the orignal article that introduced it here. The only change introduced is that I've used a closure for the hook function.

Credit


This is ESRCH's work. I've just put it on github to make it a little more accessible (it's buried deep in the Processwire forums.)

Method Of Operation


The module uses ProcessWire's ability to extend its classes by adding new hook functions to them. It adds one method sum to the Pages class. Thus, anywhere you get access to the $pages class, you can get a sum worked out from any subset of pages. The subset is defined using PWs selectors.

The summation is fast as it uses MySQL to sum the underlying data rather than having to iterate over every page in the selected set and pull out a value.

Installation


In the Processwire modules page, click the "New" tab and either enter PagesSum into the box or choose the option to install from a zip file. The zip file to use is the master branch zip from the gihub repository, here.

Example Use In Template File


If you wanted to sum a field called 'costs' across all pages using the 'job' template, you'd simply do this...

<p>Job costs £<?php echo $pages->sum('template=jobs', 'costs'); ?></p>

And if you wanted things a little more formatted, just run that through number_format first...

<p>Job costs £<?php echo number_format($pages->sum('template=jobs', 'costs'), 2); ?></p>

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.