2013-04-13 33 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

已解決? –