2013-04-13 32 views
0

我只是试图根据if()语句OR s和与$pattern的匹配来分配类。我已经尝试了几个直接比赛和其他方法。if/else条件不正确执行分配变量值

我无法根据ORpreg_match()分配$boxclr。我究竟做错了什么?

$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... 

回答

1

尝试了这一点,它优化尽我所能,感动了重复preg_match()IF S和使用in_array()两个条件,这样你就不必做多重比较。根本问题是您在条件语句中没有正确使用括号,因此排序/优先级已关闭。 preg_match()移动解决了这一问题。

$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()... 
    } 
} 
+0

神秘的ツ,哇..这是相当花哨。真的很感激它。我会给你一个镜头,让你知道。 – OldWest

+0

我还没有能够测试它,因为我正在等待一些记录来完成处理。我一定会让你知道的。感谢您的询问。真的很感激它。 – OldWest

+0

已解决? –