0

ステートメントsif()と. いくつかの直接一致と他の方法を試しました。OR$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...
4

1 に答える 1

1

これを試して、可能な限り最適化し、繰り返しをs のpreg_match()外に移動し、 2 つの条件に使用して、複数の比較を行う必要がないようにします。根本的な問題は、条件ステートメントで括弧を適切に使用していなかったため、順序/優先順位がオフになっていました。この動きはそれを修正します。IFin_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 ()...
    }
}
于 2013-04-13T12:56:23.037 に答える