Displaying Webform results in a block
Colin Calnan | Wednesday, January 14th, 2009We use the webform module for most of our form needs on our client sites. It’s a pretty good module that provides most of the functionality required. The most important part of the webforms module is the submission data. The reason for the form in the first place is to gather data that can be analyzed and then acted on. Webform has a results area where you can view all the submitted data, analyze it, download it and export it. It does this using some useful built in functions such as webform_get_submissions.
However, say you want to get the number of submissions to a particular field in a form and then display the most popular choices in a block, a little similar to a poll but the data is collected as part of a webform. For our example, the form is collecting a list of reasons for donating and then displaying the top 5 reasons in a block on the site.
One way to achieve this is via a module. We create a block using hook_block and then display the top 5 reasons in it (explanations are in comments in code):
function mymodule_block($op = 'list', $delta = 0, $edit = array()) { switch($op){ // Create the block listing in the block admin page case 'list': $blocks[0]['info'] = t('Top 5 Reasons for donating'); return $blocks; break; case 'view': switch($delta) { case 0: if (module_exists('webform')) { // Set the node id to the webform we require the results of $nid = 999; // Load the webform node $node = node_load($nid); // Retrieve the list of reason choices from the webform field, split at line breaks // The choices are entered in a textarea when creating a form // in the format "i_have_money|I have lots of money" $reason_key_pair = explode('', $node->;webform['components'][1]['extra']['items']); // Loop through the textarea to create an array in the form // $submission['i_have_money']=>;I have lots of money. foreach($reason_key_pair as $key =>; $value) { $new_array = explode('|', $value); $submissions[trim($new_array[0])] = $new_array[1]; } // Execute the query to return the top 5 reasons for donating from the webform_submitted_data table $result = db_query("SELECT sd.data reason, COUNT(sd.data) total FROM {webform_submissions} s LEFT JOIN webform_submitted_data sd ON sd.sid = s.sid WHERE sd.nid = %d AND sd.data != '%s' GROUP BY sd.data ORDER BY COUNT(sd.data) DESC LIMIT 0,5", $node->nid, ''); // Create an array which will hold the reasons - which we will then display in the block $reasons = array(); // We run through the submissions and match them to the choices from the webform textarea values while ($row = db_fetch_array($result)) { $safe_key = $row['reason']; // If a user chosen option actually exists in the current options available, then display it if(array_key_exists($safe_key, $submissions)) { $reasons[] = $submissions[$safe_key]; } } // If there are no results we need to display a message if (count($reasons) == 0) { $reasons[] = array('data' => t('There are no reasons')); } // Theme the results as a list to be styled with CSS $block['content'] = theme('item_list', $reasons, NULL, 'ol', $attributes = NULL); } break; } return $block; break; } } |
Enable the module, then go to the blocks admin page and your block will now be available. Simply set the title and position it where you want it.
An example of this block can be seen in the right sidebar on the BCNDP website.