I would like to use "additional verification" to call the database and check to see if the sum of all submitted values for a specific field in my form has exceeded a set limit. I am trying to modify the code found here: http://drupal.org/node/342896 to do this, but apparently I don't know how to call for the data from the database.

My field name in the form is "adults" -- how would I structure the database query to get the data?

Thanks

Comments

ln282’s picture

I know the cid of the data I need. I'm trying to use:

$count=array_sum(db_result(db_query("SELECT * FROM {webform_submissions} WHERE nid = %d AND cid =10", $node->nid)));

and it's not working-- is there something wrong with my syntax?

THanks for any help!

liam morland’s picture

db_result returns only a single value: the first field of the first record of a query.

You may be able to get the sum you are looking for with SQL:

$count = db_result(db_query("SELECT sum(fieldname) FROM {webform_submissions} WHERE nid = %d AND cid = 10", $node->nid));

On a related note, Webform has a function to grab entire submissions. Example use:

module_load_include('inc', 'webform', 'webform_submissions');
$values = webform_get_submissions($nid, null, $user->uid, 1);
ezheidtmann’s picture

Try webform_submitted_data instead of webform_submissions.

ln282’s picture

Thanks!
I'm using this in the "additional validation" section:

// Get the value of the relevant field from the form being currently submitted.
$total_attending = $form['submitted']['total_attending']['#value'];
// Sum the total for the relevant field in the webform_submitted_data table for this node.
  $count = db_result(db_query("SELECT sum(data) FROM {webform_submitted_data} WHERE nid = %d AND cid = 7", $node->nid)) + $total_attending;

  // The submission limit is 125 - if it has been exceeded...
  if ($count >= 125) {
    // Uncheck all role access for this node. This disables access so users will see
    // this message when they navigate to the form: 'Registrations are no longer being accepted as this event is full. Thank you for your interest.'
    $node->webform['roles'] = array();
  
    // Save the node with all roles unchecked
    node_save($node);
  }

And it works great. Adding the total from the form being currently submitted is key.

I'm going to add this to the comments on the additional validation code snippets page in the handbook.

liam morland’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.