Separate arrays in columns in an html table

Asked

Viewed 64 times

-1

I wanted to separate my array by columns in a table. When I enter the records you get a ',' but when I take the comma you get an Array to string Conversion error. Could you help me?

How I wanted the table to stay:

Date|Name|Value

2020-01-02|a|1.00€

2020-01-02|b|1.00€

Total value| 2.00€

How are you: Descrição

Here is my code:

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

    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>"); 

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

1 answer

0

I would have to understand why you store the "Cost Name" data in this way "a,b", it seems some modeling error, changing the Insert to individualize the cost name you would not have this problem.

An alternative solution:

<?php
$newArray = [];
while($registo = mysqli_fetch_assoc($resultado))
{
    $arrayNomeDoCusto = explode(',' $registro['nome_custo']);
    foreach($arrayNomeDoCusto as $key => $value)
    {
       $newArray[$key] = $registo;
       $newArray[$key]['nome_custo'] = $value;
    }

}

$newArray can now be traversed with its individuality.

Browser other questions tagged

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