ipsure logo
Logo and Language
Login icon Language selection icon
Hello, guest
*NIX BACKUP Hands-On blog header image Right block of Hands-On blog header image Final menu block of Hands-On blog header image
MS TIP PKI PROJECTS CMS Active category menu left background Active category menu right background Türkçe HANDS-ON SERVICES IT BUSINESS CONTACT ABOUT REFERENCES TERMS RSS
Home page Hands-On Services IT Business Contact About References Terms of Use RSS


How to Sum Up “Global: PHP” Field Values in Drupal 7 Views

Filed under: CMS — Tags: , , , , , — Sezgin Bayrak @ 20:02

Drupal 7In Views, summing up custom php query results among themselves and getting the totals individually can be a problematic or even impossible task if you try to do this by using aggregation method or Views Calc module. After wasting considerable amount of time with these helpless methods, I decided to go wild and finally found a convenient solution via PHP approach.


Assuming that the “machine name” of your custom Global: PHP field is php_1, all you need is to add a “Global: PHP” field in the FOOTER of your view setup and place the following PHP code inside the Output code area. If you have more than one php field in your views setup, then you need add more Global: PHP fields in the FOOTER and arrange your getsubtotal arrays with appropriate names such as php_2, php_3 and so on.

Notice that I’ve used consecutive str_replace fixes, as I have to convert thousand and decimal markers into my preferable format before printing the result. You can play with them in order to have it your way.

Don’t forget to use <?php and ?> delimiters to enclose PHP code. Utilize CSS techniques to enhance the appearance.

Output code:

$array = array();
     foreach ($view->style_plugin->rendered_fields as $getsubtotals) {
          $array[] = $getsubtotals['php_1'];

$dotfix = str_replace('.','',$array);
$commafix = str_replace(',','.',$dotfix);
$spacefix = str_replace('',',',$commafix);
$total = array_sum($spacefix). "\n";

//print the result
print "<tr class='alt'><td><font color='red''>TOTAL</font></td><td style='text-align:right;'>".number_format($total,2,",","."). " TL</td></tr>";
Related Posts with Thumbnails
Subscribe to our RSS feeds Email Subscription via FeedBurner RSS Subscription via FeedBurner
  1. Displaying the Total Number of Rows Returned by Views in Drupal 7
  2. Showing username instead of My Account in Drupal 7

No Comments »


There has not been any trackback links yet.

Reader Comments

There are currently no reader comments available at this time.

RSS feed for comments RSS feed for comments on this post. TrackBack URL

Leave a comment