Join a cell from an html table [closed]

Asked

Viewed 68 times

-1

My table is like this inserir a descrição da imagem aqui

But I wanted the total value in a single cell not to be separated into several total values. Exp:

Date|Name|value

2020-02-12|a|1.00€

2020-02-12|b|1.00€

Total Value|2.00€

Code:

<table style="width: 50%;" border='0' id="table" name="table">
<tr>
<th width="10%" align='center' bgcolor='#baba84'>Data</th>
<th width="25%" align='center' bgcolor='#baba84'>Nome do custo</th>
<th width="8%" align='center' bgcolor='#baba84'>Valor</th>
</tr>
<?php
while($registo = mysqli_fetch_assoc($resultado)){
    echo '<tr>';
        echo'<td align="left" bgcolor="white">'.$registo['data'].'</td>';
        echo'<td bgcolor="white">'.$registo['nome_custo'].'</td>';
    if($registo['valor']>=$registo['valor'].".00"){
        $valor=$registo['valor'].".00";
        echo "<td align='left' bgcolor='white'>".$valor."€</td>";
        print ("<td bgcolor='white' width='1%'><a href='alterar_custos.php?num={$registo['num']}&data={$registo['data']}&nome={$registo['nome_custo']}&valor=$valor' class='button2'>Ver</a></td>");
        print ("<td bgcolor='white' width='1%'><a href='eliminar_custos.php?num={$registo['num']}' class='button2'>Eliminar</a></td>");
    }
    else{
        echo "<td align='left' bgcolor='white'>".$registo['valor']."€></td>";
        print ("<td bgcolor='white' width='1%'><a href='alterar_custos.php?num={$registo['num']}&data={$registo['data']}&nome={$registo['nome_custo']}&valor={$registo['valor']}' class='button2'>Ver</a></td>");
        print ("<td bgcolor='white' width='1%'><a href='eliminar_custos.php?num={$registo['num']}' class='button2'>Eliminar</a></td>"); 

    }
    if($registo['valor']){
    ?>
    <tr>
    <td align='left' colspan='2' bgcolor='#baba84'><b>Valor Total</b></td>
    <?php
        $valor_total=0; 
        $valor_total = $valor_total+$registo['valor'];

        if($valor_total>=$valor_total.".00"){
            $valor_t=$valor_total.".00";
    ?>
        <td align='left' bgcolor='white'><?php echo $valor_t."€"?></td>
    <?php
        }
        else{
    ?>
        <td align='left' bgcolor='white'><?php echo $valor_total."€"?></td> 
    <?php
        }
        echo "</tr>";
    }
    echo "</tr>";
}
?>
</table>

You could help me identify the mistake I’m making to make the chart look like this.

  • Try to talk better about what you need, you’re very confused...

  • @Jaksonfischer already made an example

  • You’re a little confused, could exemplify better?

  • @Mateusd. exemplify how I made an example how I want the table

2 answers

1


Puts a Count or sizeof in your array to know how many records you have, so you will only display the total when the array is read to the end... example:

I don’t know if it’s all right, but that’s the idea, see if it helps you.

   <table style="width: 50%;" border='0' id="table" name="table">
   <thead>
        <tr>
        <th width="10%" align='center' bgcolor='#baba84'>Data</th>
        <th width="25%" align='center' bgcolor='#baba84'>Nome do custo</th>
        <th width="8%" align='center' bgcolor='#baba84'>Valor</th>
        </tr>
    </thead>
    <tbody>
<?php
$total_valor = 0;

while($registo = mysqli_fetch_assoc($resultado)){

    // nao vou mexer aqui mas da para melhorar.
    echo '<tr>';
        echo'<td align="left" bgcolor="white">'.$registo['data'].'</td>';
        echo'<td bgcolor="white">'.$registo['nome_custo'].'</td>';
    if($registo['valor']>=$registo['valor'].".00"){
        $valor=$registo['valor'].".00";
        echo "<td align='left' bgcolor='white'>".$valor."€</td>";
        print ("<td bgcolor='white' width='1%'><a href='alterar_custos.php?num={$registo['num']}&data={$registo['data']}&nome={$registo['nome_custo']}&valor=$valor' class='button2'>Ver</a></td>");
        print ("<td bgcolor='white' width='1%'><a href='eliminar_custos.php?num={$registo['num']}' class='button2'>Eliminar</a></td>");
    }
    else{
        echo "<td align='left' bgcolor='white'>".$registo['valor']."€></td>";
        print ("<td bgcolor='white' width='1%'><a href='alterar_custos.php?num={$registo['num']}&data={$registo['data']}&nome={$registo['nome_custo']}&valor={$registo['valor']}' class='button2'>Ver</a></td>");
        print ("<td bgcolor='white' width='1%'><a href='eliminar_custos.php?num={$registo['num']}' class='button2'>Eliminar</a></td>"); 

    }

    // soma todos os valores
    $total_valor += $registo['valor'];


}

?>
</tbody>
<tfoot>
    <tr>
        <td align='left' colspan='2' bgcolor='#baba84'><b>Valor Total</b></td>
        <td align='left' bgcolor='white'><?php echo number_format($total_valor, 2, ',', '.'); ?></td>
    </tr>
</tfoot>
</table>

0

  • I got it all worked out, but thanks anyway for replying.

Browser other questions tagged

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