简体   繁体   English

用两个信息对数组进行排序| 的PHP

[英]Sort array with two infos | PHP

I made code which loads online players from game server(through MySQL table). 我编写了从游戏服务器(通过MySQL表)加载在线玩家的代码。 Now, I'm newbie in PHP and I don't Know how to sort them(users) in table by ID. 现在,我是PHP的新手,不知道如何按ID对表(用户)进行排序。 I googled and I found answer which will sort arrays by value(1, 2, 3, 4 etc..). 我用谷歌搜索,发现答案将按值(1、2、3、4等)对数组进行排序。 Only problem is because users, with ID, have a name. 唯一的问题是,具有ID的用户具有名称。 How to connect user's name with ID, so they stay together after sort? 如何使用ID连接用户名,以便他们在排序后保持在一起? Here's a code 这是一个代码

while($_hsync_podatci = $_hsync_rezultat->fetch_assoc())
{
    ?>
        <tr class="_hsync_online_stil_<?php echo $_hsync_dio; ?>" id="_hsync_na_mrezi_<?php echo $_hsync_podatci['ServerID']; ?>">
            <td><?php echo $_hsync_podatci['ServerID']; ?></td>
            <td><?php echo $_hsync_podatci['Ime']; ?></td>
            <td>
                <button type="button" id="_hsync_izbaci_<?php echo $_hsync_podatci['ServerID']; ?>" class="btn btn-danger" onclick="_hsync_izbaci(<?php echo $_hsync_podatci['ServerID']; ?>)" style="float: right;">
                    <span class="glyphicon glyphicon-log-out"></span>Izbaci
                </button>
            </td>
        </tr>
    <?php
        $_hsync_dio = !$_hsync_dio;
}

Above while is table's header. 在while上方是表格的标题。 Var $_hsync_dio is for background color. 变量$_hsync_dio用于背景颜色。 One row is white, second is grey, and so on. 一排是白色,第二排是灰色,依此类推。

When doing a MySQL query you can sort the array automatically when fetching the data for example: 在执行MySQL查询时,您可以在获取数据时自动对数组进行排序,例如:

$players = mysqli_query($conn, "SELECT * FROM playerTable ORDER BY id ASC");
    while($row = mysqli_fetch_assoc($message_sender_info)){
            $message[] = $row;
          }

Use DESC instead of ASC for e reverse sorting. 使用DESC而非ASC进行反向排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM