How to add a summary line to a Drupal view?

I created a view (Drupal 6.x, Views 2.x). I would like to be able to add a summary row at the end of this view - summarize several columns and include the totals in the summary row.

How can i do this? Is there any data sorting mechanism that I can implement to modify the constructed data (before it is processed)?

(Note: I cannot use views_calc because some of the data in this view comes from Views relationships that views_calc does not support at the time of writing.)

+6
drupal drupal-views drupal-6
source share
5 answers

To answer my question in a couple of hours ... One way would be to implement hook_views_pre_render() :

 /** * Implementation of hook_views_pre_render(). */ function mymodule_views_pre_render(&$view) { if ($view->name == 'myview') { // perform calculations on each row $pointsEarned = $pointsPossible = 0; foreach($view->result as $submission) { if (is_numeric($submission->node_data_field_pointsearned_field_pointsearned_value)) { $pointsEarned += $submission->node_data_field_pointsearned_field_pointsearned_value; $pointsPossible += $submission->node_node_data_field_pointspossible_field_pointspossible_value; } } // insert a 'total' row $row = new stdClass(); $row->node_data_field_pointsearned_field_pointsearned_value = $pointsEarned; $row->node_node_data_field_pointspossible_field_pointspossible_value = $pointsPossible; $view->result[] = $row; if ($pointsPossible > 0) { // insert an 'average' row $row = new stdClass(); $row->users_name = 'Average:'; $row->node_data_field_pointsearned_field_pointsearned_value = round($pointsEarned/$pointsPossible * 100) . "%"; $view->result[] = $row; } } } 
+6
source share

Looking around, it looks like Views Calc can do what you want.

+4
source share

Views Calc contains many open bugs. Summarize browsing seems more stable.

+2
source share

Personally, I would handle this in presentation templates. Create views-view.tpl.php for your view, and then edit it to calculate and print a resume.

Another option is to create another display for the same view, and then create views-view-unformatted.tpl.php , and also calculate and print the summary without using print $row; to avoid calling the field template. Display add usage where necessary.

+1
source share

If anyone else comes across this question, you can use Views Summarize , which adds a generic table display. You simply set up the display, and then choose how you want each column to be summed. I have not been able to get this to work with Views Data Export yet, but it works if you just want to see the data on the site.

+1
source share

All Articles