首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >if/else条件没有正确执行以分配变量值

if/else条件没有正确执行以分配变量值
EN

Stack Overflow用户
提问于 2013-04-13 20:05:32
回答 1查看 101关注 0票数 0

我只是尝试根据if()语句ORs和$pattern的匹配项来分配类。我已经尝试了几种直接匹配和其他方法。

我无法根据OR的和preg_match()获取要分配的$boxclr。我做错了什么?

代码语言:javascript
复制
$row['casetype_txt'] = $var; // $var = ADDICTION or GOTOBO etc..

$pattern = "/^".$_POST["loc_type"]."/"; // ADDI or GOTO etc... 

while ($row = @mysql_fetch_assoc($result)) 
{
///////////////////////////////////////////////////
///////////////////// TYPE 1 /////////////////////////
///////////////////////////////////////////////////
if ( // TYPE 1 
preg_match($pattern, $row['casetype_txt']) 
AND 
   $row['last_action_txt'] == 'A' 
OR $row['last_action_txt'] == 'B' 
OR $row['last_action_txt'] == 'C'
OR $row['last_action_txt'] == 'D'

)
{
 $boxclr = "type1"; // assigning class
} elseif ( // TYPE 1-2
preg_match($pattern, $row['casetype_txt']) 
AND
   $row['case_disp'] == 'C' 
OR $row['case_disp_txt'] == 'E' 
OR $row['case_disp_gp'] == 'F' 
OR $row['disp_act_txt'] == 'G' 

) { 
$boxclr = "type1-2"; // assigning class
}  
///////////////////////////////////////////////////
///////////////////// TYPE 2 /////////////////////////
///////////////////////////////////////////////////
elseif ( // TYPE 2
preg_match($pattern, $row['casetype_txt']) 
AND 
$row['last_action_txt'] == 'H' 
OR $row['last_action_txt'] == 'I' 
OR $row['last_action_txt'] == 'J'
OR $row['last_action_txt'] == 'K'
)
 {
$boxclr = "type2"; // assigning class
} elseif ( // TYPE 2-2
preg_match($pattern, $row['casetype_txt']) 
AND 
   $row['case_disp'] == 'C' 
OR $row['case_disp_txt'] == 'L' 
OR $row['case_disp_gp'] == 'M' 
OR $row['disp_act_txt'] == 'N' 

)
{ 
$boxclr = "type2-2"; // assigning class
} 
///////////////////////////////////////////////////
///////////////////// TYPE 3 ////////////////////////
///////////////////////////////////////////////////
elseif...
EN

回答 1

Stack Overflow用户

发布于 2013-04-13 20:56:23

尝试一下,尽我所能对其进行优化,将重复的preg_match()移出IFs,并将in_array()用于其中两个条件,这样您就不必进行多次比较。根本问题是您没有在条件语句中正确使用括号,因此顺序/优先级是关闭的。preg_match()的移动解决了这个问题。

代码语言:javascript
复制
$row['casetype_txt'] = $var;

$pattern = '/^' . preg_quote($_POST['loc_type']) . '/'; // escape special chars

while ($row = @ mysql_fetch_assoc($result)) // I recommend not using @
{
    if (preg_match($pattern, $row['casetype_txt']))
    {
        if (in_array($row['last_action_txt'], array('A', 'B', 'C', 'D')))
        {
            $boxclr = 'type1';
        }
        else if ($row['case_disp'] == 'C' || $row['case_disp_txt'] == 'E' || $row['case_disp_gp'] == 'F' || $row['disp_act_txt'] == 'G')
        {
            $boxclr = 'type1-2';
        }
        else if (in_array($row['last_action_txt'], array('H', 'I', 'J', 'K')))
        {
            $boxclr = 'type2';
        }
        else if ($row['case_disp'] == 'C' || $row['case_disp_txt'] == 'L' || $row['case_disp_gp'] == 'M' || $row['disp_act_txt'] == 'N')
        {
            $boxclr = 'type2-2';
        }
        // ...else if ()...
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15987597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档