简体   繁体   English

排序变量以从最高到最低显示

[英]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). 如果number($ nm)落在该范围内,则下面的代码将回显用户的位置,因此,假设我有$ nm =(10,4,3,3,1),位置分别为(staff,staff,而不是(marketer,staff,staff,staff,staff)而不是(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: vice";
}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 usort将解决您的问题

but isnt your if/else if wrong ? 但是,如果不是,您的if / else是否错误?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM