0

My question is not a duplicate and it has complex conditions. Please let me explain. In my second condition, first I need to check if number value is correct (in first condition I checked complete opposite). If so, then check if there's a value in email column. Then show error message if there's a value in email column. My problem is I need to write first query (already done by me) and second query under the same 'db_select'

Checking for email value only activates if webform value matches a value in 'number' column.


I have a table containing two columns namely, 'number' and 'email'.

I need to run a db_select query to display same error message when a webform is submitted. My conditions are as follows:

  1. Show error message if number value provided by webform does not matches a value in 'number' column
  2. Show error message if number value provided by webform matches a value in 'number' column but corresponding 'email' column is not empty.

So far I'm able to show error message for the first condition.

db_set_active('mydatabase');    
    $query = db_select('mytable', 'c');
    $query->fields('c', array('number','email_address'))
    ->condition('number', $phoneval, '=');
    $result = $query->execute()->fetchAll();

if (empty($result))
{
    form_set_error('phone_number','We have no record of that Assessment Number');
} 

How can I show the same error message for condition 2? I don not want to query the database again and thinking of using for db_or this.

Thanks

1
  • My question is not a duplicate and it has complex conditions. Please see the updated question. Commented May 23, 2016 at 0:52

3 Answers 3

1

Syntax for db_or :

$or = db_or();
$or->condition();
$or->condition();
$query->condition($or);

For More Information

0
db_set_active('mydatabase');    
$query = db_select('mytable', 'c');
$query->fields('c', array('number','email_address'))
->condition('number', $phoneval, '=')
->condition('email_address', $mailval, '=');
$result = $query->execute()->fetchAll();

You could use nested if

if (empty($result)) {
  // do print_r($result);
  // here you will get the result in array
   if (empty($result['number'])) {
    form_set_error('phone_number','We have no record of that Assessment Number');
   } else if(empty($result['email_address'])) {
   form_set_error('email_address','We have no record of that email');
   }
}
1
  • Thanks but my conditions are complex than you think. As in your second condition, I'm not matching email address but only number value. first show error message if number value doesn't match 'OR' if number value matches but there's a value in email column. Commented May 23, 2016 at 0:42
0

Can't you just use PHP for this?

db_set_active('mydatabase');    
  $query = db_select('mytable', 'c')
    ->fields('c', array('number','email_address'))
    ->condition('number', $phoneval, '=');
  $result = $query->execute()->fetchAll();

// Don't forget to set the db back to default
db_set_active();

if (empty($result)) {
    form_set_error('phone_number','We have no record of that Assessment Number');
}
// We found a record with matching number... check e-mail
else {
  // Grab the first item in array
  $result = reset($result);
  // If e-mail's not empty, show error.
  if (!empty($result->email_address)) {
    form_set_error('phone_number','We have no record of that Assessment Number');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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