how to generate a table with an array of size 228

1
echo "<script>console.log(\"".sizeOf($status)."\");</script>";

This array has a size of 228.
Within this array there is nome , status1 , status2 , status3 , status4 . (5 columns).
I wanted to print them in tabular form.

if($status[$i]['numStatus'] == 1){
    echo "<tr>";
    echo "<td>".$status[$i]['Nome']."</td>";
    echo "<td>" . $status[$i]['status'] . "</td>".PHP_EOL;

} else if($status[$i]['numStatus'] == 2) {
    echo "<td>" . $status[$i]['status'] . "</td>".PHP_EOL;

} else if($status[$i]['numStatus'] == 3){
    echo "<td>" . $status[$i]['status'] . "</td>".PHP_EOL;

} else if ($status[$i]['numStatus'] == 4){
    echo "<td>" . $status[$i]['status'] . "</td>".PHP_EOL;

    echo  "</tr>".PHP_EOL;
}

I did it that way, but I think there's a better way, do not you?

    
asked by anonymous 08.07.2015 / 01:21

1 answer

1

Assuming your list will come in order, you can do something like this:

$qtdeColunas = 4; // 4 porque ele gera 2 na primeira vez, totalizando 5
$i = 0;

echo "<table>";
echo "<tr>";
for (;$i < sizeOf($status);$i++) {

    if ($i % $qtdeColunas == 0) {
        echo "</tr><tr>";
        echo "<td>".$status[$i]['Nome']."</td>";
    }
    echo "<td>" . $status[$i]['status'] . "</td>";
}

//Adiciona as tds caso tiver faltando algum pra dar as 5 colunas no final
while ($i <= $qtdeColunas) {
    echo "<td>&nbsp;</td>";
    $i++;
}
echo "</tr>";
echo "</table>";
    
08.07.2015 / 01:42