how to generate a table with an array size 228

Asked

Viewed 33 times

1

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

That one array has a size of 228.
Within that array there is nome, status1, status2, status3, status4. (5 columns).
I wanted to print them out in table 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 this way, but I think there’s a better way, isn’t?

1 answer

1


Assuming your list comes 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>";

Browser other questions tagged

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