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") 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

2 番目の並べ替え方法として価格を使用して、2 回並べ替えてみませんか?

 $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 に答える