Displaying Webform results in a block
We 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.
March 19th, 2009 at 3:46 pm
great topic. I’m new in developing using Drupal. I confuse where i must put the code. I try to put it in a block that i cretaed but not works. Could you explain more detail? Thank you.
March 19th, 2009 at 3:58 pm
Henrik,
to use this code you must first create a module, with a mymodule.module file. You can find out how to do that at http://drupal.org/node/82920. Once you have created your mymodule.module file, you then place the above code into that file. function mymodule_block is a block hook that creates a block for you to use on your site. You place the webform results in this block, and then turn the block on to view it.
I hope this helps.
Col.
March 19th, 2009 at 9:59 pm
Thank you for your fast response Colin. Now i can see my module. It works now, and i try to figure out how your code can be implemented in my case.
March 19th, 2009 at 11:17 pm
Hi Colin,
Can we used this approach to display a webform result that contain another webform? For example, i display a list/table of data resulting from webform, and in each data it will has a check box element (another webform element), so i can select one or more data to do more process.
April 23rd, 2009 at 7:48 am
Hi Colin,
This post has been helpful to me. While your use case is different than mine, I hope to be able to bend it to fit. Thanks for your gift.
Tim
July 3rd, 2009 at 5:14 am
Hi drupler,
I wann to display web-form(say its about website-enquiry) into drupal blocks. How could I do that?
Thanks in advance..
Web-Farmer
@letsnurture.com
July 6th, 2009 at 8:28 am
Hi, you should be able to find the answer to your question here http://drupal.org/node/248157. You need to programatically load the webform node in the block using PHP.
March 2nd, 2010 at 3:15 pm
Hey, thanks a lot. This was not exactly I needed. I needed the survey statistics submitted by users on the admin side. I modified your code.
Thank you so much.