SHOW:
         |
         |
         - or go back to the newest paste.    
    | 1 | - | // http://pastebin.com/kHpaHQvi originally | 
| 1 | + | <?php | 
| 2 | // http://www.gravityhelp.com/forums/topic/show-total-donations | |
| 3 | // usage: [donations form=37] where 37 is the form ID | |
| 4 | add_shortcode('donations', 'total_donations');
 | |
| 5 | function total_donations($atts) {
 | |
| 6 | $form_id = $atts['form']; | |
| 7 | // function to pull all the entries for one form | |
| 8 | $donations = RGFormsModel::get_leads($form_id); | |
| 9 | // start the total at zero | |
| 10 | $total = 0; | |
| 11 | // initialize a counter for the number of donations made | |
| 12 | $i = 0; | |
| 13 | // loop through all the returned results | |
| 14 |         foreach ($donations as $amount) {
 | |
| 15 | // add each donation amount to the total | |
| 16 | - | // change 3 here to the field ID which holds the donation amount | 
| 16 | + | // change [5] here to the field ID which holds the donation amount | 
| 17 | - | $total += $amount[3]; | 
| 17 | + | // the preg_replace will strip anything that is not a number or decimal | 
| 18 |                 $numeric_amount =  preg_replace('/[^0-9\.]/Uis', '', $amount[5]);
 | |
| 19 | $total += $numeric_amount; | |
| 20 | // increment the counter so we know how many donations there are as well | |
| 21 | $i++; | |
| 22 | } | |
| 23 | // change this to your locale, or, if your locale is already defined, you can remove this line | |
| 24 | setlocale(LC_MONETARY, 'en_GB'); | |
| 25 | // do some formatting and return the html output from the shortcode | |
| 26 |         $output = "We have raised " . money_format('%i', $total) . " from $i donors.";
 | |
| 27 | // just the string above will be returned. You can style it here or where you are using the shortcode | |
| 28 | return $output; | |
| 29 | } | |
| 30 | // needed for the above to process the donation shortcode in sidebar widget | |
| 31 | add_filter('widget_text', 'do_shortcode'); | 
