没有在sql中进行排序价格

时间:2012-08-24 08:50:40

标签: php mysql

我在做一张票价表。 除了一个人之外,它完美地工作,价格以错误的顺序出现,这是我到目前为止所拥有的:

$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
    }                                   
}

3 个答案:

答案 0 :(得分:1)

为什么不使用价格作为第二种排序方法进行两次排序?

 $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())

答案 1 :(得分:0)

我不得不说在mysql中对价格进行排序,它会更容易,更快速,更高效。是否有特定原因导致您不想在sql中进行排序?

当然,您可以按ticket_type DESC订购,价格为ASC / DESC。

答案 2 :(得分:0)

我与sammaye提出了建议,但我会回答这个问题,因为它可能对某些自定义D-base或C-base系统很有用,它不支持多个订单文件。

所以你可以用数组排序来做到这一点:

$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());

   $results = array();//instanciate array
    while($row = mysql_fetch_array($totalresult)){ 
        $type = $row['ticket_type'];//get current type to preserve type order
        $price = $row['ticket_price'];//get the current price to sort by price
        if(!is_array($results[$type])){
            $results[$type]=array();//current type is new
        }
        $results[$type][$price] = $row;//store entire row datas     
    }

    $types = array_keys($results);
    $nb = count($types);
    for($i=0;$i<$nb;$i++){//get the current type, preserving group
        $type = $types[$i];
        $prices = array_keys($results[$types]);//extract prices of the group
        asort($prices);//sort prices ASC
        //arsort($prices);//sort price DESC
        foreach($prices As $price){
            $row = $results[$types][$price];//now you can get you'r datas and start you'r process
        }
    }