Drupal Questions & Answers

Ask and answer Drupal related questions! No question too simple or complex!
Innovative Mapping Solutions's picture
Asked by on Wed 8th Aug 2012
Sum Not NULL fields using PHP

I am trying to sum how many fields of five are not null. For example:

field_method1 = apple
field_method2 = NULL
field_method3 = orange
field_method4 = banana
field_method5 = NULL

I am trying to sum all entries for the content type ‘fruit’ such that the above would be ‘3’

My end goal is to give a summary such as:

1 of 5: 215
2 of 5: 300
3 of 5: 128
4 of 5: 288
5 of 5: 109

This is why I do not care what the particular value is for a given field, I am only concerned with whether or not there is a value there.

I have tried to accomplish this with Views using the aggregate function, but have not been able to. I assume the best option would be to use a computed field to calculate this as items are entered. This is OK, but it doesn’t necessarily need to be stored in the database, so it may not be the best solution.

My problem is that I have very little PHP skills. I think this solution is relatively simple, for someone with solid skills in PHP. Any help with the code, or suggestions how to accomplish this in a simpler / better way I would really appreciate the help.

Cheers!

Commenting on this Drupal Question is closed.