Handle single record while

1

Considering that I'm developing a small simple (photo) ranking, I need a solution to get the first 3 records from the select and apply individual formatting to them, the rest unnecessarily. Note: I do not wish to have to use 2 lops, one way maybe using arrays, correct?

Ihavethefollowingcode:

<?php$selPlacares=$conn->prepare("SELECT * FROM jogadores j LEFT JOIN placares p ON p.pla_jog_id = j.jog_id GROUP BY j.jog_id ORDER BY p.pla_cartas DESC, p.pla_tempo ASC, j.jog_nome ASC");
        $selPlacares->execute();
        $cont = $selPlacares->rowCount(); ?>

        <table border="0" align="left" width="70" cellpadding="5" cellspacing="5">
        <tr style="background: #58589E;">
            <td>Pos.</td>
        </tr>

        <tr style="background: #E7BD40;">
            <td>1º</td>
        </tr>

        <tr style="background: #c9c9c9; color: #000;">
            <td>2º</td>
        </tr>

        <tr style="background: #623825;">
            <td>3º</td>
        </tr>

        <?php for($s = 4; $s <= $cont; $s++): ?>
        <tr>
            <td><?php echo $s."&deg;"; ?></td>
        </tr>
        <?php endfor; ?>

        </table>

        <table border="0" align="left" width="85%" cellpadding="5" cellspacing="5">
        <tr style="background: #58589E;">   
            <td>Nome do jogador</td>
            <td>Quant. Cartas</td>
            <td>Tempo</td>
        </tr>

        <?php while($rowPlacares = $selPlacares->fetch(PDO::FETCH_OBJ)): ?>
        <tr>
            <td><?php echo $rowPlacares->jog_nome; ?></td>
            <td><?php echo $rowPlacares->pla_cartas; ?></td>
            <td><?php echo $rowPlacares->pla_tempo; ?></td>
        </tr>
        <?php endwhile; ?>


        </table> 
    
asked by anonymous 21.05.2014 / 19:34

3 answers

2

One of the most painless ways to do this with your current code is by using a counter within while which you would use as a condition to apply some CSS class, for example.

<?php

$cursor = 0;

while($rowPlacares = $selPlacares->fetch(PDO::FETCH_OBJ)):

$cursor++;

switch( $cursor ) {

    case 1: $trColor = 'red'; break;
    case 2: $trColor = 'green'; break;
    case 3: $trColor = 'blue'; break;
    case default: $trColor = 'black'; break;
}

?>

<tr style="background-color: "<?php echo $trColor; ?>">
    <td><?php echo $rowPlacares->jog_nome; ?></td>
    <td><?php echo $rowPlacares->pla_cartas; ?></td>
    <td><?php echo $rowPlacares->pla_tempo; ?></td>
</tr>
<?php endwhile; ?>

It's not very elegant, but you used PDOStatement :: fetch () instead of first building the structure with PDOStatement :: fetchAll () and then iterating it, it's an output.

    
21.05.2014 / 19:46
5

You have two ways to do this via css, the simplest is this:

See Fiddle

table tr:nth-child(1){
background-color:#bbccaa
}
table tr:nth-child(2){
background-color:#aabbcc
}
table tr:nth-child(3){
background-color:#bbaacc
}

In this way, the first row of the table will have the style applied in table tr:nth-child(1) , second table tr:nth-child(2) and third table tr:nth-child(3)

    
21.05.2014 / 19:46
4

You can do this only with CSS , using the nth-child () property, example:

table tbody tr:nth-child(1) td{background-color:blue;}
table tbody tr:nth-child(2) td{background-color:green;}
table tbody tr:nth-child(3) td{background-color:yellow;}

JSFiddle Example

    
21.05.2014 / 19:45