0

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

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

3 回答 3

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())
于 2012-08-24T08:57:44.797 回答
0

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

当然,您可以通过ticket_type DESC、价格ASC/DESC 进行订购。

于 2012-08-24T08:58:12.083 回答
0

同意 sammaye的建议,但我会回答这个问题,因为它可以用于一些不支持多个订单文件的自定义 D 基或 C 基系统。

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

$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
        }
    }
于 2012-08-24T09:57:45.137 回答