首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >怎样才能按字母顺序列出项目?

怎样才能按字母顺序列出项目?
EN

Stack Overflow用户
提问于 2016-02-17 19:30:00
回答 2查看 185关注 0票数 0

我希望列出项目(或在本例中的游戏标题)的字母顺序。

使用php,我从mysql数据库中获取游戏,我使用foreach轻松列出所有游戏,但我需要它按字母顺序列出它们。

games.php

代码语言:javascript
复制
<table style="width:100%;" class="hover">
    <th style="width:5%;">A-Z</th>
    <th style="width:70%;">Games</th>
    <th style="width:10%;">Clans</th>
    <th style="width:10%;">Recruiting</th>


    <tr>
        <td></td>
        <td>None Specified</td>
        <td></td>
        <td></td>
    </tr>
<?php 
    $query = " 
        SELECT 
            az,
            games,
            clans,
            recruiting
        FROM games 
    "; 

    try 
    { 
        // These two statements run the query against your database table.
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll(); 
?> 


<?php foreach($rows as $row): ?> 
    <tr>
        <td><?php echo $row['az']; ?></td>
        <td><a href="/games/"><?php echo $row['games']; ?></a></td>
        <td><?php echo $row['clans']; ?></td>
        <td><?php echo $row['recruiting']; ?></td>
    </tr>
<?php endforeach; ?>


<tr>
    <th style="width:5%;">A-Z</th>
    <th style="width:70%;">Games</th>
    <th style="width:10%;">Clans</th>
    <th style="width:10%;">Recruiting</th>  
</tr>
</table>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-17 19:32:36

只需在查询中下单:

代码语言:javascript
复制
$query = " 
    SELECT 
        az,
        games,
        clans,
        recruiting
    FROM `games` 
    ORDER BY games ASC
"; 
票数 1
EN

Stack Overflow用户

发布于 2016-02-17 19:33:02

您需要将您的MySQL更新为

代码语言:javascript
复制
SELECT 
    az,
    games,
    clans,
    recruiting
FROM games 
ORDER BY games ASC
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35465951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档