Paul
Paul

Reputation: 3368

Ordering by a selected column from database

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.

i.e.:

1

2

3, etc.

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();
}
?>

Upvotes: 0

Views: 63

Answers (3)

Dipen Shah
Dipen Shah

Reputation: 1919

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

Upvotes: 3

MaggsWeb
MaggsWeb

Reputation: 3027

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. :)

Upvotes: 1

Anonymous
Anonymous

Reputation: 12090

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.

Upvotes: 0

Related Questions