订购变量以从最高值到最低值显示

时间:2011-08-22 15:47:47

标签: php

如果数字($ nm)下降,下面的代码将回显用户的位置 在这个范围之间让我说我有$ nm =(10,4,3,3,1)的位置将分别是(员工,员工,员工,营销人员,员工)而不是(营销人员,员工,员工,员工,员工) )我如何安排让营销人员先行(营销人员,员工,员工,员工,员工)。

$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: vice";
}else if($nm <= 30){
echo "Position: manager";
}else if($nm <= 20){
echo "Position: marketer";
}else if($nm <= 10){
echo "Position: staff";
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

usort可以解决您的问题

但不是你的if / else如果错了吗?

不应该吗?

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