How to paint cell according to the value of a select?

Asked

Viewed 634 times

4

I am in need of a help to format a <td> according to the outcome of select PHP. I thought about using jQuery but I don’t know much about it.

<tr class="">
    <td class=""><?php echo ($row['Id_acordo']); ?></td>
    <td class=""><?php echo ($row['Contrato']); ?></td>
    <td class=""><?php echo ($row['Carteira']); ?></td>
    <td class=""><?php echo ($row['DataPgto']); ?></td>
    <td class=""><?php echo ($row['Valor']); ?></td>
    <td class=""><?php echo ($row['AvistaOuParcelado']); ?></td>
    <td class=""><?php echo ($row['QuitacaoOuAtualizacao']); ?></td>
    <td class=""><?php echo ($row['FormaEnvio']); ?></td>
    <td class=""><?php echo ($row['Recuperador']); ?></td>
    <td class=""><?php echo ($row['Observacao']); ?></td>
    <td class=""><?php echo ($row['Cadastrado']); ?></td>
</tr>

The tags <td> I need you to change class according to the condition:

<?php if($row['Cadastrado'] == '0'){ ?>
    <script>$('td').addClass("success");</script>
<?php }else{ ?>
    <script>
       $('td').removeClass("success");
       $('td').addClass("danger");
    </script>   
<?php   }   ?>

Only it either paints everything by adding the two classes in the tag <td> or add none. Example below:

<td class="success danger">16</td>

I’ve tried instead of if else put two loops of if, but it wasn’t.

If possible put only in jQuery without integration with the <?php>, because I would like him to update himself with the function $(document).ready.

  • From what I understand, you want to color the lines of your table according to the result that comes from the bank? For example Registered ==0 blue line, registration == 1, green line and so on. That would be it?

  • In case the select and the table are on the same page and you want a real-time update, or is it coming from a form and are two different pages? If it’s two pages, you can do it by putting ifs within itself td, kind of: <td class="<?php if($row['Cadastrado'] === '0'){ echo 'success'; } else { echo 'danger'; } ?> ">16</td>. Now if you’re on the same page, you’ll need to use Ajax.

  • 1

    On the same page are the Select and the table, and really the intention is to update in real time. I’ll see how to do in ajax. Thank you, gustavox And that’s really it Marconi, I want to paint the td according to the values 123. Thank you.

2 answers

0


Try to use the class bg-success:

inserir a descrição da imagem aqui

Source: W3school

  • I don’t think that’s what he wants, I think his problem is coloring each line according to the bank result.

  • So this would be a part of the fix, because the bootstrap contains the class bg-Success that is responsible for "painting" the background of a cell ;D

  • 1

    It worked, I needed to paint the background of the cell, using the Ternario operator. Thank you very much.

0

Hello, I believe that’s it... replace the <td> who wishes to paint for this:

<td class="<?php echo ($row['Cadastrado'] == 0 ? 'success' : 'danger' ); ?>">

Browser other questions tagged

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