Advertisement
chrishajer

Shortcode to total up wedding dinner selections

Apr 15th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2. // Help Scout 52777
  3. // shortcode to grab dinner selection totals from one form by value
  4. // usage: [dinner choice='whatever' formid=ID]
  5. // eg [dinner choice='Panko Cod' formid=892]
  6. if (!function_exists('ch_get_dinners')) {
  7.     function ch_get_dinners($atts) {
  8.         // parse the shortcode
  9.         extract(shortcode_atts(array(
  10.             'choice' => null,
  11.             'formid' => null
  12.         ), $atts));
  13.  
  14.         // define constants
  15.         $paging = array('offset' => 0, 'page_size' => 100 );
  16.         $sorting = array();
  17.         $total_count = 0;
  18.  
  19.         // define search criteria
  20.         $search_criteria = array();
  21.         $search_criteria["field_filters"][] = array('value' => $choice);
  22.  
  23.         // get the entries which match the dinner choice
  24.         $entries = GFAPI::get_entries($formid, $search_criteria, $sorting, $paging, $total_count);
  25.  
  26.         // initialize the counter
  27.         $counter = 0;
  28.  
  29.         // define which fields to check for dinner selections
  30.         $all_fields = array(22, 24, 52, 61, 66);
  31.  
  32.         // loop through all the entries
  33.         foreach($entries as &$entry){
  34.             // check our 5 possible fields
  35.             foreach ($all_fields as &$field_id){
  36.                 if ($entry[$field_id] == $choice){
  37.                     $counter++;
  38.                 }
  39.             }
  40.         }
  41.         return $counter;
  42.     }
  43. }
  44. add_shortcode('dinner', 'ch_get_dinners');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement