我只是试图根据if()
语句OR
s和与$pattern
匹配来分配类。我试过几种直接匹配和其他方法。
我不能让$boxclr
基于OR
和preg_match()
进行分配。我做错了什么?
$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...
尝试一下,尽我所能进行优化,将重复的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 ()...
}
}