Hey,
I was wondering if anyone knew where I was going wrong with my code. I need to find the sum of some fields using the computed field module. I have a field called field_hours_spent_working. This field is repeated every time someone does a bit of work. I would like another field below this that works out the total hours worked (adding up the field_hours_spent-working).
Here's the code:
$node_field[0]['value'] = db_result(db_query("SELECT SUM(field_hours_spent_working) FROM {content_type_type_new_design_job}"));
And here's the display code:
$display_output = $entity_field_item['value'];
I have put this code in a computed field. However the field_hours_spent_working field is within a field collection. Would this affect my code? Any help on this would be great!I can't seem to find an answer anywhere so please let me know if you have a suggestion.
Thanks
Comments
I am guessing you only want
I am guessing you only want to sum values for the current node, so how about
The code you posted
sums all values for the field (not just the current node) and would run before the node is saved so it would not account for changes to the node being added/edited.
Thanks for the reply
Would I simply put this in the computed field? Thanks again for the quick reply. Love the Drupal community.
Thanks
Tried but no luck
Hey,
I tried your code but nothing seems to show up. Would you suggest using another field type? I don't even get the field showing up at all.
Thanks
I actually tried the code
I actually tried the code (though my field names where different so there may be a type). Try change the code to
The code assumes you are not using languages.
When saving a node of this content type you should see the two messages.
Where to put the code
Hey,
I was wondering where I put this code? Does it get placed in to a standard text field or a computed field type?
Sorry not really the PHP developer.
Thanks
It goes in a computed field.
It goes in a computed field.
Summing up of Multiple value field collection item field
I have multivalued Field collection field , user can create un limted time periods using
a "time period" field type 'date',with start and end dates
a "duration" field in months is populated as the date difference between start and end dates,
Each time user creates a new "duration", i want to populate the sum of total "durations" field, I tried using the code here, result is given for only for that filed collection field and not for all the filed collection durations, please help
summing up Multi value filed collection item computed filed
I have a computed field in multi value field collection
each time a filed is added, i want to get a total value in another field
any one plz help