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

3

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:

However,Iamabouttocustomizemytable,givingalternatingcolorstotr.ButIwantedtodoitdifferently:IwantedtogivedifferentcolorsofacorNºComanda,andtheendresultwouldlooksomethinglikethis:

How to proceed in this case?

    
asked by anonymous 04.10.2017 / 13:35

2 answers

1

You can create a variable / flag to see if the number has changed and change color accordingly.

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 
}
    
04.10.2017 / 14:02
1

Based on what happened, I'll pass on the concept of how I would:

As you said, that can vary a lot, so basically you would have to have a mapa de cores in relation to the right numbers! For only "sortear" a cor , could one be equal to another (almost impossible but can happen)

1.1 - It would create a array

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

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

2.3 - You would draw a "hexa" to a cor aleatória

2.4 - Check if that color already exists in the array

2.5 - If there is, draw another, if not, save in array

To search the array, use array_search

    
04.10.2017 / 14:18