3

Am using the following code in the Congratulations page. There is more but this is the driving force to retrieve user submissions. Its working fine but I need to retreive as well the actual component labels. How do I include the actual questions (labels) on the form. Like if I have a component:

How old are you

  • 10
  • 25

How do I modify the code below to show "How old are you" on the congratulations page?

<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); 
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
    for ($i = 1; $i <= 2; $i++) {
        echo $submission->data[$i]['value'][0];
    }
?>

1 Answer 1

12

The loaded submission doesn't hold the label names so you'll have to get them from elsewhere. One option would be to load the webform node where they can be found. Here's an example about how this would look like in the code:

<?php
include_once(drupal_get_path('module', 'webform') .'/includes/webform.submissions.inc');
$nid = arg(1); 
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
$node = node_load($nid);
foreach ($node->webform['components'] as $key => $component) {
  if (isset($submission->data[$key]['value'][0])) {
    if ($component['type'] == 'select') {
      $options = array();
      $items = explode("\n", $component['extra']['items']);
      foreach ($items as $item) {
        $item_parts = explode('|', $item);
        if (isset($item_parts[0]) && isset($item_parts[1])) $options[$item_parts[0]] = $item_parts[1];
      }
      $no = count($submission->data[$key]['value']);
      if ($no > 1) {
        $cont = '<p><strong>'. $component['name'] .':</strong> ';
        foreach ($submission->data[$key]['value'] as $k => $v) {
          if ($k + 1 == $no) $cont .= $options[$v];
          else $cont .= $options[$v] .', ';
        }
        $cont .= '</p>';
        echo $cont;
      }
      elseif (isset($options[$submission->data[$key]['value'][0]])) {
        echo '<p><strong>'. $component['name'] .':</strong> '. $options[$submission->data[$key]['value'][0]] .'</p>';
      }
    }
    else {
      echo '<p><strong>'. $component['name'] .':</strong> '. $submission->data[$key]['value'][0] .'</p>';
    }
  }
}
?>
7
  • OMG!! Cant thank you enough. I popped your codes in the congrats page and miracles happened! I dont even need my old codes anymore! Thanks a million! One more thing. What if I want to get the exact description of the chosen option value instead of option numbers?
    – Albert
    Feb 27, 2012 at 19:58
  • @Albert Dealing with select options, checkboxes and radio buttons means a good chunk of extra code in this case. I updated my answer to cover those cases as well now.
    – Madis
    Feb 27, 2012 at 23:01
  • Thanks so much. It worked but also threw this error "Notice: Undefined offset: 1 in eval() (line 14 of". Line 14 contains: "$options[$item_parts[0]] = $item_parts[1];"
    – Albert
    Feb 28, 2012 at 18:57
  • @Albert Replacing that line with if (isset($item_parts[0]) && isset($item_parts[1])) $options[$item_parts[0]] = $item_parts[1]; should fix it.
    – Madis
    Feb 28, 2012 at 20:01
  • That worked! Thank you very much. I think my problem is 100% solved!
    – Albert
    Feb 29, 2012 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.