Piers Blinco Piers Blinco - 2 days ago 3
MySQL Question

sorting price without doing it in sql

I am doing a ticket pricing table.
It works perfectly apart from one niggle, the prices are coming out in the wrong order, here is what i have so far:

$totalresult = mysql_query("select * from ticket_pricing WHERE (service_id='".mysql_real_escape_string($_POST['service_id'])."') and (boarding_point='".mysql_real_escape_string($_POST['boarding_point'])."') order by ticket_type DESC") or die(mysql_error());

while($row = mysql_fetch_array($totalresult)){
if(strtolower($row['ticket_type']) === "single"){
if (!$a++){
echo($row['ticket_type']);
$a++;
}
?>
<span>
<?php echo($row['price']);?>
</span>
<?php
}
if(strtolower($row['ticket_type']) === "return"){
if (!$b++){
echo("<br />" . $row['ticket_type']);
$b++;
}
?>
<span>
<?php echo($row['price']);?>
</span>
<?php
}
if(strtolower($row['ticket_type']) === "period"){
if (!$c++){
echo("<br />" . $row['ticket_type']);
$c++;
}
?>
<span>
<?php echo($row['price']);?>
</span>
<?php
}
if(strtolower($row['ticket_type']) === "group"){
if (!$d++){
echo("<br />" . $row['ticket_type']);
$d++;
}
?>
<span>
<?php echo($row['price']);?>
</span>
<?php
}
}

Answer

Why not just sort twice over using the price as the second method of sort?

 $totalresult = mysql_query("select * from ticket_pricing  
     WHERE (service_id='".mysql_real_escape_string($_POST['service_id'])."') 
     and (boarding_point='".mysql_real_escape_string($_POST['boarding_point'])."') 
     order by ticket_type DESC, price ASC") or die(mysql_error())