Join equal records in an HTML table

Asked

Viewed 612 times

2

I have a table that returns several records.

Table Code:

<table>
    <thead>
        <tr>
          <th>N° Prog</th>
          <th>Data Opr</th>
          <th>Origem</th>
          <th>Destino</th>
          <th>Remetente</th>
          <th>Peso</th>
          <th>Peso Total</th>
          <th>Destinatário</th>
          <th>Previsão de Chegada</th>
          <th>Veículo</th>
          <th>Placa</th>
          <th>Motorista</th>
          <th>Observação</th>
          <th>St</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($controller->ListaTudo($objProg) as $objProg){ ?>
       <tr>
         <td><?php echo $objProg->getprog(); ?></td>
         <td><?php echo $objProg->getdataopr(); ?></td>
         <td><?php echo $objProg->getorig(); ?></td>
         <td><?php echo $objProg->getdest(); ?></td>
         <td><?php echo $objProg->getremetente(); ?></td>
         <td><?php echo $objProg->getpeso(); ?></td>
         <td><?php echo $objProg->getpesottl(); ?></td>
         <td><?php echo $objProg->getdestinatario(); ?></td>
         <td><?php echo $objProg->getprev(); ?></td>
         <td><?php echo $objProg->getplaca(); ?></td>
         <td><?php echo $objProg->getcarreta(); ?></td>
         <td><?php echo $objProg->getmot(); ?></td>
         <td><?php echo $objProg->getobs(); ?></td>
         <td><?php echo $objProg->getst1(); ?></td>
       </tr>
   <?php } ?>
   </tbody>
</table>

inserir a descrição da imagem aqui

I need that when the N° Prog (in case the three 74 numbers in the image) are equal, type one rowspan in td, that there is only one "74" but the rest remains 3 lines. However, there will not always be 3 equal records, there may be more or only one.

  • Create a logic first, counting the same values and save as rowspan, before mounting the foreach. After, do the assembly bringing this result.

2 answers

2

the property of <html> that you seek is the "rowspan" she is linked to <td>, is a tag that accumulates the amount of lines that repeat the same value, in your case 3, then in the first line <tr> accumulates the total number of repetitions per line <td rowspan="3">74</td> in the same column and in the next rows you cannot display this value (or <td>):

<table>
    <thead>
        <tr>
          <th>N° Prog</th>
          <th>Data Opr</th>
          <th>Origem</th>
          <th>Destino</th>
          <th>Remetente</th>
          <th>Peso</th>
          <th>Peso Total</th>
          <th>Destinatário</th>
          <th>Previsão de Chegada</th>
          <th>Veículo</th>
          <th>Placa</th>
          <th>Motorista</th>
          <th>Observação</th>
          <th>St</th>
        </tr>
    </thead>
    <tbody>
      <tr>
        <td rowspan="3">74</td>
        <td>12/07/2016</td>
        <td>BTO</td>
        <td>SPO</td>
        <td>MOVEIS K1</td>
        <td>13300.00</td>
        <td>23400.00</td>
        <td>SID LAR</td>
        <td>19/07/2016</td>
        <td></td>
        <td>ISW-5946</td>
        <td></td>
        <td></td>
        <td>1</td>
      </tr>
      <tr>
        <td>12/07/2016</td>
        <td>BTO</td>
        <td>SPO</td>
        <td>UNICASA</td>
        <td>4100.00</td>
        <td>23400.00</td>
        <td>SID LAR</td>
        <td>19/07/2016</td>
        <td></td>
        <td>ISW-5946</td>
        <td></td>
        <td></td>
        <td>1</td>
      </tr>
      <tr>
        <td>12/07/2016</td>
        <td>BTO</td>
        <td>SPO</td>
        <td>UNICASA</td>
        <td>600.00</td>
        <td>23400.00</td>
        <td>SID LAR</td>
        <td>19/07/2016</td>
        <td></td>
        <td>ISW-5946</td>
        <td></td>
        <td></td>
        <td>1</td>
      </tr>
    </tbody>
</table>

1


With the help of the other answers, I found a solution:

The way it is there, you need to sweep the valuables before giving the echo with another looping.

Behold:

EDITED

        <?php 
     echo "<td>";
    // acredito que o method ListaTudo retorna um array certo? =D
    // então coloquei na variável prog
    $prog = $controller->ListaTudo($objProg);

    // quantidade listada
    $quantProg = count($prog);

    // arrays que iremos usar no looping for
    $valor = array(); // este array contem o valor da variavel
    $rowArray = array(); // este array vamos registrar os rows
    $row = 1; // este array quantas x ele se repete

    // vamos varrer os valores antes do foreach

    for($x = 0; $x <= $quantProg; $x++){

        if($x < $quantProg){

            // insere valor
            $valor[$x] = $prog[$x] -> getprog();

        }

        if($x > 0) {

            $y = $x -1;

            if(isset($valor[$x]) &&  $valor[$x] == $valor[$y]){

                $row++;

            } else {

                if($x < $quantProg){

                    $w = $x - $row;

                } else {

                    $w = $quantProg - 1;

                }

                echo "<td rowspan='".$row."'>".$valor[$w]."</td>";

                foreach ($prog as $objProg){ 

                    echo "<td>".$objProg->getdataopr()."</td>
                    <td>".$objProg->getorig()."</td>
                    ... </tr>"; 
                    // insira todas as variáveis do foreach...

                }

                $row = 1;

            }

        }


    }

    ?>

Of course there could be mistakes because I didn’t take the test. But the logic would be this.

I did it this way but the ideal is to have a function that returns it to you.

  • I understood only returned me this error Undefined offset: -1 in the if($quantRow[$x] == $quantRow[$y]){

  • tries now @Kevin. F ! I did the opposite. I put the foreach within the for. This allowed to improve unification. I created some arrays to test and it worked

Browser other questions tagged

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