Handle single while record

Asked

Viewed 146 times

1

Considering that I am developing a small ranking of simple classification (photo), I need a solution to take the first 3 records of select and apply an individual formatting to them, the others without need. Note: I do not wish to have to use 2 Ops, one way may be using arrays, correct?

modelo

I have the following code:

<?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> 

3 answers

5

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

Vide 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
}

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

  • Actually, much more practical, I will use this form because it is more practical, however, the first answer exclaimed how we can do using PHP. Thank you for your reply.

4

You can do it just with CSS, using the property Nth-Child(), 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

  • 1

    Really with CSS is much easier. but he wanted to know how to do with PHP. Do what right?

  • I agree, CSS is easier and solves problems. I didn’t necessarily want to know in PHP I just didn’t remember this function in CSS so I thought there was no way to solve this in CSS. It was good to know the ways, thank you!

  • Not for that reason :)

2


One of the most painless ways to do this with your current code is to use a counter inside the 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 assembling the structure with Pdostatement::fetchAll() and then re-injecting it, it’s an output.

  • Actually this way it works, but with simpler CSS. When I asked the question I didn’t necessarily want the answer in PHP, however, it was interesting to know how to solve this problem. I will use CSS because it is simpler, I already knew the function of this code but used to cross lines, I had not thought about it. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.