循环查询

时间:2010-08-14 18:42:03

标签: php performance

    //display faction userlevel:

$level_boss = $req_faction_info['f_boss'];
$level_uboss = $req_faction_info['f_uboss'];
$level_rhm = $req_faction_info['f_rhm'];
$level_lhm = $req_faction_info['f_lhm'];
$level_r1 = $req_faction_info['f_r1'];
$level_r2 = $req_faction_info['f_r2'];

if($level_boss == $username){ $u_level = 'Boss'; }
elseif($level_uboss == $username){ $u_level = 'Underboss'; }
elseif($level_rhm == $username){ $u_level = 'Right Hand Man'; }
elseif($level_lhm == $username){ $u_level = 'Left Hand Man'; }
elseif($level_r1 == $username){ $u_level = 'Recruiter One'; }
elseif($level_r2 == $username){ $u_level = 'Recruiter Two'; }
else{ $u_level = 'Faction Member'; }

echo '<div id="faction_userlevel">Your current level within the faction is: '.$u_level.'</div>';

必须有一种更简单的方法吗?

3 个答案:

答案 0 :(得分:1)

怎么样:

$levels = array(
    'f_boss' => 'Boss',
    'f_uboss' => 'Underboss',
    'f_rhm' => 'Right Hand Man',
    'f_lhm' => 'Left Hand Man',
    'f_r1' => 'Recruiter One',
    'f_r2' => 'Recruiter Two'
);
$level = null;
foreach($levels as $key => $val){
   if($username == $req_faction_info[$key]) $level = $levels[$key];
}
if($level === null) $level = 'Faction Member';

echo '<div id="faction_userlevel">Your current level within the faction is: '.$level.'</div>';

答案 1 :(得分:0)

好的......这样做:

foreach($req_faction_info AS $key => $val){
    $$key = $val;
}

switch $username{
    case $f_boss:
        $u_level = "Boss";
        break;

    case $f_uboss:
        $u_level = "Underboss";
        break;
}
嗯......想不出别的办法真的这么做。

答案 2 :(得分:0)

$level=array(
    'Faction Member',
    'f_boss'=>'Boss',
    'f_uboss'=>'Underboss',
    'f_rhm'=>'Right Hand Man',
    'f_lhm'=>'Left Hand Man',
    'f_r1'=>'Recruiter One',
    'f_r2'=>'Recruiter Two'
);
echo '<div id="faction_userlevel">Your current level within the faction is: '.$level[array_search($username,$req_faction_info)].'</div>';