views:

35

answers:

3
    //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>';

There must be an easier way of doing this?

A: 

Ok... do this:

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;
}

hmm... can't think of any other way to really do this.

Thomas Clayson
+1  A: 

how about :

$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>';
darma
thank you! I'm surprised at how fast I got an answer! :D
Callum Johnson
A: 
$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>';
stillstanding
but you miss the "Faction Member" case.
darma
Easy as pie. Answer fixed. array_search will return FALSE if it doesn't find a match. FALSE will correspond to index element 0 in the array.
stillstanding