How to toggle table colors by grouping by a specific table field?

Asked

Viewed 320 times

2

I have the following code.

<table style="width:100%;">
  <thead>
    <tr>
      <th scope='col'>&nbsp;&nbsp;Nº Comanda&nbsp;&nbsp;</th>
      <th scope='col'>&nbsp;&nbsp;Produto&nbsp;&nbsp;</th>
      <th scope='col'>&nbsp;&nbsp;Quantidade&nbsp;&nbsp;</th>
      <th scope='col'>&nbsp;&nbsp;V. Unitário&nbsp;&nbsp;</th>
      <th scope='col'>&nbsp;&nbsp;V. Total&nbsp;&nbsp;</th>
      <th scope='col'>&nbsp;&nbsp;Registrado Por:&nbsp;&nbsp;</th>
      <th scope='col'>&nbsp;&nbsp;Data & Hora:&nbsp;&nbsp;</th>
    </tr>
  </thead>
  <tfoot>
<?php
$buscarucomanda=mysql_query("SELECT cont_estoques_cadp.descricao, 
                                        cadastro_geral.nome_com, 
                                        cadastro_geral.id, 
                                        comanda.status, 
                                        comanda.quantidade, 
                                        comanda.valorUnit, 
                                        comanda.valortt, 
                                        comanda.data_op, 
                                        comanda.id_user,
                                        comanda.num_comanda 
                                        FROM 
                                        cadastro_geral 
                                        JOIN
                                        comanda
                                        JOIN 
                                        cont_estoques_cadp 
                                        ON 
                                        comanda.id_user = cadastro_geral.id 
                                        and 
                                        cont_estoques_cadp.id=comanda.id_produto 
                                        WHERE comanda.status='0' 
                                        order by comanda.num_comanda ASC");
if(mysql_num_rows($buscarucomanda) == 0){
  echo "<tr>
  <td colspan='6'>
  <center>
  Não foi possível localizar os produtos registrados. <br>
  Consulte seu supervisor para mais informações!
  </center>
  </td>
  </tr>";
}else{
  while($linha=mysql_fetch_array($buscarucomanda)){
    ?>
    <tr> 
      <td><?php echo $linha['num_comanda'];?></td>
      <td><?php echo utf8_encode($linha['descricao']);?></td>
      <td><?php echo $linha['quantidade'];?></td>
      <td><?php echo $linha['valorUnit'];?></td>
      <td><?php echo $linha['valortt'];?></td>
      <td><?php echo utf8_encode($linha['nome_com']);?></td>
      <td><?php echo date('d/m/Y',strtotime($linha['data_op']));?> às <?php echo date('H:i',strtotime($linha['data_op']));?></td>
    </tr>
    <?php 
  }
}
?>
</tfoot>
</table>

which returns the following result.tabela_comanda

However, I am in order to customize my table, giving alternate colors to tr. But I wanted to do it differently: I wanted to give different colors of color Nº Comanda, and the end result would be more or less like this:

resultado final

How to proceed in this case?

  • Each of these "Nº Comanda" would have a different color? You have already decided which number gets which color?

  • What is the condition of the record to change the color, because you mention the column, but in your example are column-specific records?

  • The condition to change the color would be: Each 'Nº Comanda' different, the table would change the color. I have thought about defining a color for each number in a static way, however this command number is currently between 0 and 200, but can change in the future to 0 and 400 or 0 and 1000 and I would like to leave something dynamic only in the result display.

  • Do you mean that the command 1 has a color 2 has another and so on? Colors cannot be repeated?

  • It could be one color. like Yellow. The same is in this image: https://i.stack.Imgur.com/Akdwb.png - But if 1 has a color, 2 would not have, 3 would have the same color as 1, but 4 would have the same color as 2. Understand?

  • Ah, you want to create a zebra effect every time the number changes is that?

  • Exactly.....

Show 2 more comments

2 answers

1


You can create a variable/flag to know if the number has changed and with that change color.

Example:

$corEmUso = 'white';
$ultimaComanda = null;
while($linha=mysql_fetch_array($buscarucomanda)){
    if (is_null($ultimaComanda)) $ultimaComanda = $linha['num_comanda']; // só na primeira vez
    if ($ultimaComanda != $linha['num_comanda']) {
        $corEmUso = $corEmUso == 'white' ? 'lightgrey' : 'white';
    }
    ?>
    <tr style="background-color: <?=$corEmUso;?>"> 
      <td><?php echo $linha['num_comanda'];?></td>
      <td><?php echo utf8_encode($linha['descricao']);?></td>
      <td><?php echo $linha['quantidade'];?></td>
      <td><?php echo $linha['valorUnit'];?></td>
      <td><?php echo $linha['valortt'];?></td>
      <td><?php echo utf8_encode($linha['nome_com']);?></td>
      <td><?php echo date('d/m/Y',strtotime($linha['data_op']));?> às <?php echo date('H:i',strtotime($linha['data_op']));?></td>
    </tr>
    <?php 
}
  • I tried to use his code, however he returned me https://s1.postimg.org/4ptx27e8nz/777.png - The colors alternate only in the first line of each different number

  • I saw that commented // only the first time, however how could it happen at all?

  • @Webcraft this line is only to work well before $ultimaComanda have received the first value of $linha['num_comanda']. That is, to know later when the number changes. So this line is only useful 1 time and should be anyway.

0

Based on what you’ve been through, I’ll pass on the concept of how I would:

Like you said it could vary a lot, so basically you’d have to have a mapa de cores with regard to the right numbers !? For only "sortear" a cor, could one become the same as the other (almost impossible but can happen)

1.1 - Create a array

2.1 - In the loop, check if the number already exists in the array, if yes, pull the color

2.2 - If not, add the drawn number to the array

2.3 - Draw one "hexa" for a cor aleatória

2.4 - Check whether that color already exists in the array

2.5 - If it exists, draw another, if not, saved in the array

For searching the array, use the array_search

Browser other questions tagged

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