Advertisement
chrishajer

Count submitted radio buttons and change confirmation

Mar 26th, 2013
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. // http://www.gravityhelp.com/forums/topic/advanced-conditional-logic#post-185385
  3. // change the 370 here to your form ID
  4. add_filter('gform_confirmation_370', 'add_up_radio_choices', 10, 4);
  5. function add_up_radio_choices($confirmation, $form, $lead, $ajax){
  6.    
  7.     // initialize and empty array to hold the questios we want to count the answers of
  8.     $questions = array();
  9.  
  10.     // my questions were inputs 1 to 10 in the $lead array
  11.     // add each answer from $lead[1] to $lead[10] to the questions array
  12.     for($i = 1; $i <= 10; $i++)
  13.         $questions[] = $lead[$i];
  14.  
  15.     // this will count the frequency of each answer in the array of questions
  16.     $count = array_count_values($questions);
  17.  
  18.     // initialize a, b and c in case no one selects them as an answer
  19.     $a = 0;
  20.     $b = 0;
  21.     $c = 0;
  22.  
  23.     // make sure the keys exist in the array, i.e. someone submitted that option as an answer
  24.     if(array_key_exists('A', $count))
  25.         $a = $count['A'];
  26.     if(array_key_exists('B', $count))
  27.         $b = $count['B'];
  28.     if(array_key_exists('C', $count))
  29.         $c = $count['C'];
  30.  
  31.     // if we had mostly A
  32.     if ($a > $b && $a > $c) {
  33.         $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>You selected mostly A.</p></div>";
  34.     }
  35.  
  36.     // if we had mostly B
  37.     elseif ($b > $a && $b > $c) {
  38.         $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>You selected mostly B.</p></div>";
  39.     }
  40.  
  41.     // if we had mostly C
  42.     elseif ($c > $a && $c > $b) {
  43.         $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>You selected mostly C.</p></div>";
  44.     }
  45.  
  46.     // what if none of the above were true? what if there were equal numbers for some choices?
  47.     else {
  48.         $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>I didn't plan for this.</p></div>";
  49.     }
  50.    
  51.     // add the total for each choice to the confirmation for debugging
  52.     $confirmation .= "<div>A: $a<br />B: $b<br />C: $c<br /></div>";
  53.  
  54.     // always return the confirmation
  55.     return $confirmation;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement