Join equal records in an HTML table

2

I have a table that returns me multiple records.

Cogido Table:

<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>

IneedwhentheProgNo.(inthiscasethethreenumbers74oftheimage)areequal,oftypearowspanintd,thatthereisonlya"74" but the rest remain 3 lines. However, it will not always be 3 equal records, they may be more or only one.

    
asked by anonymous 15.07.2016 / 13:35

2 answers

1

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

As it is there, you need to scan the values before giving echo with another looping.

See:

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 may be errors because I did not take the test. But the logic would be that.

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

    
15.07.2016 / 21:09
2

The property of the <html> that you are looking for is "rowspan" it is linked to <td> , it is a tag that accumulates the number of rows that repeats of the same value, in its case 3, then in the first row <tr> accumulates the total number of replicates per line <td rowspan="3">74</td> in the same column and in the next lines can not 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>
    
15.07.2016 / 15:36