将开关大小写与 &&& 组合以包含其他变量



我在这里使用一些PHP,该php根据联系表7中的下拉列表发送电子邮件。我希望它仅执行并发送电子邮件,如果其他三个输入之一是数字10

我尝试将开关与& amp;结合起来。和||运算符,但无法根据需要执行。我在这里受到学习的限制,因此我感谢您可能会提供的任何帮助。

/* CF7自动回复开关 */

挂钩到WPCF7_MAIL_SENT-这将在提交表单后发生

add_action('wpcf7_mail_sent','contact_form_autor vessecters'(;

我们的自动回复函数

function contact_form_autoresponders( $contact_form ) {
    if( $contact_form->id==14 ){ #your contact form ID - you can find this in contact form 7 settings
        #retrieve the details of the form/post
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();                          
        #set autoresponders based on dropdown choice            
        switch( $posted_data['location'] ){ #your dropdown menu field name
                    case 'AB':
                    case 'AL':
                    case 'B':
                    case 'BA':
                    case 'BB':
                    case 'BD':
                    $msg="email a";
            break;
                case 'NW':
                case 'N':
                case 'E':
                case 'W':
                case 'SW':
                case 'SE':
                case 'WC':
                case 'EC':  
           ``` && if ( $posted_data['size1'] == 10 || $posted_data['size2'] == 10 || $posted_data['size3] == 10)  ```    
            $msg="email b";
            else $msg="email a";
            break;
        }

        #mail it to them
        mail( $posted_data['femail-610'], 'Thanks for your enquiry', $msg );
    }
}

因此,如果从表单下拉列表中选择了任何第一个案例,则我希望发送电子邮件'a'如果选择了任何次要情况,则发送电子邮件为'b'否则,它发送电子邮件为" A"。

您可能应该使用in_array()进行此类检查。

只需将您的开关更改为IF语句,然后定义2个数组,并使用要比较的值。

$locationsForEmailA = ['AB', 'AL', 'B', 'BA', 'BB', 'BD'];
$locationsForEmailB = ['NW', 'N', 'E', 'W', 'SW', 'SE', 'WC', 'EC'];
if(in_array($posted_data['location'], $locationsForEmailA, true) || 
    in_array($posted_data['location'], $locationsForEmailB, true) && 
    !($posted_data['size1'] == 10 || $posted_data['size2'] == 10 || $posted_data['size3'] == 10)){
    $msg = "email a";
} else {
    $msg = "email b";
}

一个小的代码分析:

....
case 'SE':
case 'WC':
case 'EC':  

直到这里,从上一个break开始,上面的case指令的开关为a或( ||(。如果将下一部分更改为:

   if ( $posted_data['size1'] == 10 
        || $posted_data['size2'] == 10 
        || $posted_data['size3] == 10 )
   {
        $msg="email b";
   }

...然后,如果至少有一个有效,则整个if条件仅对上述情况⇒$msg="email b"执行。/p>

切换根本不工作。

您需要切换到if/else-if模式或使用查找字典。

最新更新