按数据库中的选定列排序 [英] Ordering by a selected column from database

查看:178
本文介绍了按数据库中的选定列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表,表示赢/输记录并有排名。我遇到了两个问题。

I have the following table that is meant to show a win/loss record and to have rankings. I'm running into two issues with it though.


  • 第一个问题是我的等级< td> 没有像我想要的那样进展。我的意思是对于每个循环和输出的记录,我希望它被编号为。

  • First issue is that my rank <td> is not progressing like I want it to. What I mean by that is for each record that is looped and output, I want it to be numbered for.

ie:

1

2

3,等等


  • 我无法弄清楚的第二部分是我希望总胜率最高的人排名最高。我也希望将损失配置到其中。因此,如果有人是5-0,他们的排名将高于5-1的人。

有人指出我遇到了正在遇到的这些问题吗?

Could someone point me in the right direction with these issues I am running into?

  <h2>Division 1</h2>
            <table>
                <tr>
                    <th>Rank</th>
                    <th>Name</th>
                    <th>Wins</th>
                    <th>Losses</th>
                </tr>
<?php
try {
    //Prepare
    if ($stmt = $con->prepare("SELECT * FROM team_rankings WHERE `division`=1")) {

        $stmt->execute();
        $stmt->bind_result($ranking_id, $ranking_user_id, $ranking_firstname, $ranking_username, $ranking_division, $ranking_wins, $ranking_losses); 

        //var_dump($stmt);

        if (!$stmt) {
            throw new Exception($con->error);
        }

    $stmt->store_result();
        while ($row = $stmt->fetch()) {
?>

            <tr>
                <td>1</td>
                <td><?php echo $ranking_firstname; ?></td>
                <td><?php echo $ranking_wins; ?></td>
                <td><?php echo $ranking_losses; ?></td>
        </table>
<?php       
        }
        }   else {
            echo "<p>There aren't any players in division 1 yet.</p>";
            }
}
catch (Exception $e)
{
    echo "Error: " . $e->getMessage();
}
?>


推荐答案

您需要使用 ORDER BY 喜欢这个

这样做:

SELECT * FROM team_rankings WHERE `division`=1" ORDER BY Wins DESC, Losses

更多: 链接

对于您的第一个问题,< / table> 需要超出您的php 循环。还有一个计数器,你可以递增并显示订单。

For your first question you </table> needs to be out of your php while loop. Also have a counter which you can increment and show the order.

$count = 0;
while ($row = $stmt->fetch()) {
?>
    <tr>
        <td><php echo $count; $count++; ?></td>
        <td><?php echo $ranking_firstname; ?></td>
        <td><?php echo $ranking_wins; ?></td>
        <td><?php echo $ranking_losses; ?></td>
    </tr>      
<?php } ?>

</table>

还有什么更好的?使用 foreach

Something better? Use foreach

这篇关于按数据库中的选定列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆