Screen Shot 2013-09-12 at 5.21.58 PM One of my clients has a handout that she uses in her speaking engagements to help people identify their learning styles. She also had the handout posted on her website. She asked me to make the handout more interactive so that people could get their results tabulated for them online instead of tabulating their own results manually.

Lizabeth, one of our web developers, built a form in Gravity Forms to collect the answers to the 15 questions. I needed to tabulate the answers once the form was submitted and then display those answers to the user so that they could better understand their learning style.

I used the Gravity Forms confirmation filter to add my code for tabulating the scores and display the results back to the user. Lizabeth set the form up to display a confirmation message on the same page once the form was submitted. I added the following code to the functions.php file for the child theme:

//
add_filter("gform_confirmation_3", "custom_confirmation", 10, 4);

function custom_confirmation($confirmation, $form, $lead, $ajax){
	$results = array();
	for ($i=1;$i<16;$i++) {
		$vals = explode(', ',$lead[$i]);
		foreach ($vals as $val) {
			$results[$val]++;
		}
	}
	arsort($results);
	$confirmation = 'Your learning styles results:<br /><table border="1" cellpadding="5px" width="50%" align="center"><tr>';
    foreach ($results as $key => $val) {
	    $confirmation .= '<th>'.$key.'</th>';
    }
    $confirmation .= '</tr><tr>';
    foreach ($results as $key => $val) {
	    $confirmation .= '<td align="center">'.$val.'</td>';
    }
    $confirmation .= '</tr></table><p style="font-size:14px">For more information about your particular learning styles triad, check out <a href="/effecientbrain-cd1">Effective Communication I CD</a>. If you want more information about any of my other products, please visit <a href="http://www.efficientbrain.com/store" shape="rect">our online store</a>.</p>';
    
    return $confirmation;
}
//

Lizabeth set the values to the radio buttons as combinations such as ‘VKA, VAK’. Basically, one answer could mean more than one learning style, so I wanted that answer to count for each learning style. My final result should have an array with each of my 6 learning styles and a count for each like this:

//
Array (
    'VAK' => 6,
    'VKA' => 6,
    'KAV' => 4,
    'KVA' => 2,
    'AVK' => 1,
    'AKV' => 1
)
//

The first part of the code loops through the Gravity Form lead to get the results into the array using the learning style as the key. I had to ‘explode’ each answer to get the multiple learning types for each answer. Then I increment the value in the array for that learning style using $results[$val]++;

Since I want my results table to be in order from largest to smallest, I use arsort($results) to put the learning style with the most answers first in the table. I then build my $confirmation string with a table of my results from most likely learning style to least likely. This string will be returned to Gravity Forms which will display the table to the user as the confirmation message.

You can see the form in action at my clients site – EfficientBrain.com

3 thoughts on “Tabulating Answers from a Gravity Forms Form”

  1. Hi there!

    I’ve been pretty much looking for this everywhere, however, since I’m not yet very skilled with PHP and gravity forms I have a question about retrieving values from the form.. how do I go about that? The $lead object documentation isn’t very extensive :\

  2. Doh, just when I posted the above..

    $lead[{field_id}]; is all I need to retrieve specific field values. Cindy, I love you though for pointing me to the right direction!

Leave a Comment

Your email address will not be published. Required fields are marked *