Paint table row when receiving specific data

Asked

Viewed 955 times

2

I have this code:

<?php
    foreach ($controller->Lista() as $objProg) {
?>
    <tr>
        <td><?php echo $objProg->getplaca(); ?></td>
        <td><?php echo $objProg->getmot(); ?></td>
        <td><?php echo $objProg->getsaida(); ?></td>
        <td><?php echo $objProg->getorig(); ?></td>
        <td><?php echo $objProg->getdest(); ?></td>
        <td><?php echo $objProg->getprev(); ?></td>
        <td><?php echo $objProg->getcarga(); ?></td>
        <td><?php echo $objProg->getadfin(); ?></td>
        <td><?php echo $objProg->getagen(); ?></td>
        <td><?php echo $objProg->getmal(); ?></td>
        <td class="t1" ><div><?php echo $objProg->getobs(); ?></div></td>
        <td><a href="edita.php?id=<?php echo $objProg->getid();?>"><p>Alterar</p></a></td>
    </tr>
<?php
    }
?>

I need that when the echo $objProg->getdest(); for SP paint every row of the table in one color. Any suggestions on how to do this ? I need javascript anyway or is there some other method ?

2 answers

8


Can make a if on the tag tr.

<tr 
    <?php if ($objProg->getdest() == 'SP') { echo 'class="linha_destaque"'; } ?>
>

and creates a highlighted string class in css

.linha_destaque{ background-color: red }

an example of table html

<table>
  <thead>
    <tr>
      <th>Coluna 1</th>
      <th>Coluna 2</th>
    </tr>
  </thead>
  <tbody>
    <tr class="linha_destaque">
      <td>L1 - C1</td>
      <td>L1 - C2</td>
    </tr>
    <tr style="background-color: green">
      <td>L2 - C1</td>
      <td>L2 - C2</td>
    </tr>
    <tr>
      <td>L3 - C1</td>
      <td>L3 - C2</td>
    </tr>
    <tr>
      <td>L4 - C1</td>
      <td>L4 - C2</td>
    </tr>
  </tbody>  
</table>
  • 1

    +1 for the 59th speed :D

  • 2

    would suggest for a space before class: ' class="..., so if it uses PHP inline it will be more "round" the HTML, generating <td> or <td class="..."> .

  • I did that but I think my css in the tag <tr> I put up a !important after the red to see, but nothing. I tested just putting <tr class="..."> without php but not going.

  • Any other test I can do ?

  • Have a look http://jsbin.com/xuwacorisa/edit?html,css,output

  • Okay, I get it. The exit is happening like this <tr class="linha_destaque">, but is not painting the line. I tried to use in <td> and painted, but the line is not. I do not know what this can be.

  • Are you using any other css files that have some rules for tr ? And in this file that put the rule . highlighted line is being loaded last?

  • 1

    I tested in another table without any css just with this and it worked. I will look for the css that is preventing. Thanks for the help.

Show 3 more comments

2

Add an if na <tr> which checks if the state is SP if yes, add the class you want to paint the line, do not do anything.

<tr class="<?php if($objProg->getdest() == 'SP') echo 'especial'; ?>">

The Saida is something like:

<tr class="especial">

Or

<tr class="">
  • Missing close php tag

  • @Felipeasunción thanks for the correction.

  • I tried this way but the same as the answer above.

Browser other questions tagged

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