Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // http://www.gravityhelp.com/forums/topic/advanced-conditional-logic#post-185385
- // change the 370 here to your form ID
- add_filter('gform_confirmation_370', 'add_up_radio_choices', 10, 4);
- function add_up_radio_choices($confirmation, $form, $lead, $ajax){
- // initialize and empty array to hold the questios we want to count the answers of
- $questions = array();
- // my questions were inputs 1 to 10 in the $lead array
- // add each answer from $lead[1] to $lead[10] to the questions array
- for($i = 1; $i <= 10; $i++)
- $questions[] = $lead[$i];
- // this will count the frequency of each answer in the array of questions
- $count = array_count_values($questions);
- // initialize a, b and c in case no one selects them as an answer
- $a = 0;
- $b = 0;
- $c = 0;
- // make sure the keys exist in the array, i.e. someone submitted that option as an answer
- if(array_key_exists('A', $count))
- $a = $count['A'];
- if(array_key_exists('B', $count))
- $b = $count['B'];
- if(array_key_exists('C', $count))
- $c = $count['C'];
- // if we had mostly A
- if ($a > $b && $a > $c) {
- $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>You selected mostly A.</p></div>";
- }
- // if we had mostly B
- elseif ($b > $a && $b > $c) {
- $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>You selected mostly B.</p></div>";
- }
- // if we had mostly C
- elseif ($c > $a && $c > $b) {
- $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>You selected mostly C.</p></div>";
- }
- // what if none of the above were true? what if there were equal numbers for some choices?
- else {
- $confirmation = "<div id='gforms_confirmation_message' class='gform_confirmation_message_370'><p>I didn't plan for this.</p></div>";
- }
- // add the total for each choice to the confirmation for debugging
- $confirmation .= "<div>A: $a<br />B: $b<br />C: $c<br /></div>";
- // always return the confirmation
- return $confirmation;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement