dongshan2004 2016-05-22 18:53
浏览 43
已采纳

使用两个信息排序数组| PHP

I made code which loads online players from game server(through MySQL table). Now, I'm newbie in PHP and I don't Know how to sort them(users) in table by ID. I googled and I found answer which will sort arrays by value(1, 2, 3, 4 etc..). Only problem is because users, with ID, have a name. How to connect user's name with ID, so they stay together after sort? 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. Var $_hsync_dio is for background color. One row is white, second is grey, and so on.

  • 写回答

1条回答 默认 最新

  • duanmiao6695 2016-05-22 19:31
    关注

    When doing a MySQL query you can sort the array automatically when fetching the data for example:

    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?