I have the following if statement. I want to sort by a variable called $slippercent
variable if $slippercent
is between 2.5 and 4 show in top and the rest next.
Let say i have the following records:
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
I want to show as follows
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>";
}
I want to sort or Order if $slippercent
is between 2.5 and 4 to show on top any order after
Use this:
ORDER BY slipercent BETWEEN 2.5 AND 4 DESC, slipercent ASC
Booleans are treated as false = 0, true = 1
, and you can use that in an ORDER BY
clause.
$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>
";
}else{
$str .= $key . "<br>
";
}
}
echo $str;
Edited to add you wanted to output the other values outside of 2.5 and 4