douhe6181 2019-03-21 21:43
浏览 51

PHP订购奖杯[复制]

This question already has an answer here:

I'm looking to order trophies by their type using PHP.

$trophies = $engine->query("SELECT name, detail, type FROM trophy WHERE group=:group");
$trophies->execute(array(':group'=>$group));

if($trophies->rowCount() > 0) {
    while ($trophy = $trophies->fetch(PDO::FETCH_ASSOC)) {
        echo $trophy['name'] . ': ' . $trophy['type'] . '<br>';
    }
} else {
    echo 'No trophies to be displayed.';
}

Right now, the trophies aren't being ordered.

Trophies can have the following types: platinum, gold, silver, bronze.

I would like to display platinum first, then gold, then silver and then bronze.

How can I do this? Is it possible to modify the SQL query to order the trophies to behave like the desired way?

</div>
  • 写回答

2条回答 默认 最新

  • dsrw29618 2019-03-21 21:54
    关注

    Or if neither of those are useful you can create extra columns in your select :

    SELECT ...., IF(type = 'platinum',1,0) as plat, IF(type='gold',2,0) AS gold...etc then use a multiple order by like ORDER BY play, gold .... etc

    评论

报告相同问题?