TheEditor TheEditor - 3 years ago 100
PHP Question

Explode and foreach to create multi <td> table

I'm using

explode()
to basically take apart paragraphs into individual words. Works great. The looping through with a
foreach()
. Also works great. Nothing complicated here.

$title_pieces = explode(" ", $title_fixed);
foreach($title_pieces as $tpiece){
echo "<b>$tpiece<br>";
}


Unfortunately this returns just an ugly long list of words. What I'd like to do but can't quite figure out how is to put this all in a nice table. Creating the table is no problem, the part I can't figure out is how to get it to write more than one
$tpiece
per row. I'd like to have maybe 5
<td>
s in each row.

So if I do:

foreach($title_pieces as $tpiece){
echo "<tr><td>$tpiece</td></tr>";
}


I'm still just left with a long list. Can someone point me in the right direction here.

Answer Source

Just a sample-code. Work around with the modulo-operator.

<?php
$i = 1;
echo '<table><tr>';
foreach($title_pieces as $tpiece){
    if ($i % 10 == 0)
        echo "</tr><tr>";

    echo "<td>$tpiece</td>";

    $i++;
}

echo '</tr></table>';
?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download