Order a variable to display from the highest to lowest value
my code below will echo the position of the user if the number($nm) fall between the range so let say i have $nm = (10,4,3,3,1) respectively the position will be (staff,staff,staff,marketer,staff) instead of (marketer,staff,staff,staff,staff) how can i arrange so that marketer come first (marketer,staff,staff,staff,staff).
$query = "SELECT DISTINCT username,num FROM user ORDER BY num DESC LIMIT 5";
$result = mysql_query ($query) or die('query error');
while( $line = mysql_fetch_assoc($result)){
$um = $line[username];
$nm = $line[num];
echo "name:$um";
if($nm <= 50){
echo "Position: president";
}else if($nm <= 40){
echo "Position: vic开发者_开发百科e";
}else if($nm <= 30){
echo "Position: manager";
}else if($nm <= 20){
echo "Position: marketer";
}else if($nm <= 10){
echo "Position: staff";
}
while( $line = mysql_fetch_assoc($result)){
$ums[] = $line[username];
$nms[] = $line[num];
}
usort($nms);
foreach ($nms as $nm){
if($nm <= 50){
echo "Position: president";
}else if($nm <= 40){
echo "Position: vice";
}else if($nm <= 30){
echo "Position: manager";
}else if($nm <= 20){
echo "Position: marketer";
}else if($nm <= 10){
echo "Position: staff";
}
}
usort would solve your problem
but isnt your if/else if wrong ?
shouldnt it be ?
if($nm <= 10){
echo "Position: staff";
} else if($nm <= 20){
echo "Position: marketer";
} else if($nm <= 30){
echo "Position: manager";
} else if($nm <= 40){
echo "Position: vice";
} else if($nm <= 50){
echo "Position: president";
}
精彩评论