如何按变量自定义值对结果进行排序

时间:2016-05-08 06:04:49

标签: php

我有以下if语句。我希望按$slippercent变量变量进行排序,如果$slippercent介于2.5和4之间,则显示在顶部,其余部分显示在下面。

假设我有以下记录:

record A = `$slippercent`=1
record B = `$slippercent`=2.6
record C = `$slippercent`=2.5
record D = `$slippercent`=2.0
record E = `$slippercent`=5
record F = `$slippercent`=4
record G = `$slippercent`=2.8
record H = `$slippercent`=3

我想表现如下

record C
record B
record G
record H
record F

其余的命令并不重要

 $record = mysqli_query($con,"SELECT * FROM tbl ORDER BY (Tread_Design='$rtdesign') DESC, (Manufacturer='$rbrand') DESC,Brand_Name");
   while ($row = mysqli_fetch_array($record)) {
  if (preg_match("/\b".preg_quote($txtbrand)."\b/i", $row['Brand_Name']) && ($row['OD']>=$small && $row['OD']<=$large) && ($txtbrand != "" && $row['OD'] != "" && $row['Brand_Name']!="") ) 

{   
$frontrc= $row['RC'];
$slippercent = round(((($frontrc*$it2t1)/$rearrc)-1)*100,2);
$slippercent2 = round(((($frontrc*$it2t1)/$rearrc)-1)*2000, 2);

echo "<td style='width:60px;  text-align:left; vertical-align: middle;'>";echo $row['SW'];"</td>"; 
echo "<td style='width:60px; text-align:left;vertical-align: middle;'>";echo $row['OD'];"</td>";
echo "<td id='slippercent' style='width:80px;  text-align:center;'><center>";echo$slippercent;echo'%';"</center></td>";

 }

如果$slippercent介于2.5和4之间,我希望排序或订购,以便在

之后显示任何订单

2 个答案:

答案 0 :(得分:0)

使用此:

ORDER BY slipercent BETWEEN 2.5 AND 4 DESC, slipercent ASC

布尔值被视为false = 0, true = 1,您可以在ORDER BY子句中使用它。

答案 1 :(得分:0)

$array=['recordA' => '1',
               'recordB' => '2.6',
               'recordC' => '2.5',
               'recordD' => '2.0',
               'recordE' => '5',
               'recordF' => '4',
               'recordG' => '2.8',
               'recordH' => '3'];

sort($array);

foreach ($array as $key => $value){
   if ($value >= 2.5 && $value <= 4){
        echo $key . "<br>\n";
   }else{
        $str .= $key . "<br>\n";
   }
}
echo $str;

编辑添加您想要输出2.5和4之外的其他值