Advertisement
chrishajer

Use shortcode to total two different fields wedding

Feb 25th, 2013
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. // http://pastebin.com/kHpaHQvi originally
  2. // http://www.gravityhelp.com/forums/topic/counter
  3. // usage: [guests form=1] where 1 is the form ID
  4. // from http://www.gravityhelp.com/forums/topic/counter#post-154024
  5. add_shortcode('guests', 'total_guests');
  6. function total_guests($atts) {
  7.         $form_id = $atts['form'];
  8.  
  9.         // function to pull all the entries for one form
  10.         $guests = RGFormsModel::get_leads($form_id);
  11.  
  12.         // start the totals at zero
  13.         $adults = 0;
  14.     $children = 0;
  15.  
  16.         // initialize a counter for the number of guests entries made
  17.         $i = 0;
  18.  
  19.         // loop through all the returned results
  20.         foreach ($guests as $amount) {
  21.                 // total the adults
  22.                 // change 2 here to the field ID which holds the number of adults
  23.                 $adults += $amount[2];
  24.  
  25.         // total the children
  26.         // change 5 here to the field ID which holds the number of children
  27.         $children += $amount[5];
  28.  
  29.                 // increment the counter so we know how many total guess there are
  30.                 $i++; // we may not need to use this
  31.         }
  32.         // do some formatting and return the output from the shortcode
  33.         $output = "So far $adults adults and $children kids are attending the wedding";
  34.  
  35.     // just the string above will be returned.  
  36.     // You can style it here or where you are using the shortcode
  37.         return $output;
  38. }
  39. // needed for the above to process the guests list shortcode in sidebar widget
  40. add_filter('widget_text', 'do_shortcode');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement