How to do ranking with PHP

3

Well, I have a problem, I know how to pull the ranking and everything.

My doubt is that I'm going to pull the 6 with the highest score, and I wanted the top 3 to have a different background, like:

Whats the first thing: yellow;

O second: blue;

Third-place: red;

The rest: gray;


How can I do this?

Code:

<?php
$rank = mysql_query("SELECT * FROM forum ORDER BY pontos DESC LIMIT 6") or die(mysql_error()); ?> 
 <?php $i = 0; while($row = mysql_fetch_assoc($rank)){ $i++; ?>
<div style="background: gray;width: 60px;height: 20px;padding: 20px;">
<?php echo $row['username']; ?> com <?php echo $row['pontos']; ?>
</div>
<?php } ?>
    
asked by anonymous 26.07.2015 / 04:17

1 answer

6

Work out as follows:

<?
    #Pontuação
    function coresPts($posicao){

        switch($posicao){

            case 0: 
                $cor = "#FFCC00";
                break;

            case 1:
                $cor = "#000000";
                break;

            case 2:
                $cor = "#CCCCCCC";
                break;

        }

        return $cor;

    }

# Buscando pontuação
$rank = mysql_query("SELECT * FROM forum ORDER BY pontos DESC LIMIT 0, 6") or die(mysql_error());

    $i = 0;
    while($row = mysql_fetch_assoc($rank)){
        $i++; 
?>
<div style="background: <? echo coresPts($i); ?>;width: 60px;height: 20px;padding: 20px;">
    <?php echo $row['username']; ?> com <?php echo $row['pontos']; ?>
</div>
<? } ?>

Explanation: We created a function called coresPts which will search for the score according to increment of $i . And then we return the correct color for each of the positions, just change the $cor of each position. The 0 position would be the first position, 1 the second position and 2 the third position.

    
26.07.2015 / 04:51