Ordering by a selected column from database

岁酱吖の 提交于 2019-12-12 01:42:44

问题


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.

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

i.e.:

1

2

3, etc.

  • The second part I cannot figure out is I want the person with the highest win total to be ranked the highest. I also want losses to be configured into that. So if someone is 5-0, their rank will be higher than someone's that is 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();
}
?>

回答1:


You need to use ORDER BY like this

Do this:

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

More: link

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>

Something better? Use foreach




回答2:


The numbering issue can be resolved as such:

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

This sets a variable $i to '1' (outside the loop), echos the variable as the number in your <td>, and increments the variable $i++ before the loop is closed.

Oh yeah, and don't close your <table> inside the loop. :)




回答3:


Looks like you want to use the ORDER BY clause in your query. It can sort by a column directly or by some calculation. For example, you could do:

SELECT *
FROM team_rankings
WHERE division = 1
ORDER BY
    (wins - losses) DESC,
    wins DESC

This way 1-0 would be ranked higher than 6-6 but lower than 2-1. How appropriate this is is up to you to decide.



来源:https://stackoverflow.com/questions/31680311/ordering-by-a-selected-column-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!