5

I would like to show a "cumulative total" of nodes by date. For example, it should look like this:

date number total
1/1  1      1
1/2  3      4
1/3  2      6
1/4  4      10

I've tried views_php without luck: enter image description here

3
+50

Your solution is correct. However, there's a bug in PHP code fields. You must access the $data variable because $row is (surprisingly) empty.

This issue will help you.

How to use PHP Code field

| improve this answer | |
  • thank you. That did the trick. I just added this as the "output code": <?php $static = $static + $data->nid; print $static; ?> – uwe Nov 14 '12 at 5:00
  • sorry, bounty can't be awarded until tomorrow ;-) – uwe Nov 14 '12 at 5:00
7

Have you tried the Views Calc module? I think you will get what you want from it. Here is an excerpt about it from the module's project page:

... adds simple calculations to a views table. Requires the Views module.

You can SUM, AVERAGE, COUNT, MIN, MAX, or AVG selected columns. A plug in theme makes the view look more spreadsheet-like (right justifies numeric fields, shades calculated columns and rows, and underlines calculated rows).

| improve this answer | |
  • I looked at it but it appears that you can only do sums or counts on all rows of a column. – uwe Nov 9 '12 at 16:24
3

I built a module for this specific issue, as even the maintainers of Views PHP say it shouldn't be used. My module is Views Cumulative Field, and there are versions for D7 and D8.

Some more details about it (from its project page):

This module creates a Views field handler that enables you to calculate the cumulative value of another field in your view. This is great for Views displays created by the Charts module, but can be used for any View that allows fields.

It was inspired by this comment on the Views PHP module page:

While this module allows you to directly use PHP inside views which may be useful for quick and easy solutions, it is highly advisable to use regular handlers and plugins when available (or even to create one yourself).

| improve this answer | |
  • Nice + interesting (and great for using it in combination with charts! – Pierre.Vriens Mar 7 '17 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.