0

我习惯于explode()将段落分成单独的单词。效果很好。用foreach(). 也很好用。这里没有什么复杂的。

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

不幸的是,这只会返回一长串难看的单词。我想做但不知道如何把这一切放在一张漂亮的桌子上。创建表没问题,我不知道的部分是如何让它$tpiece每行写一个以上。我希望<td>每行可能有 5 秒。

所以如果我这样做:

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

我仍然只剩下一长串清单。有人可以在这里指出我正确的方向吗?

4

2 回答 2

0

尝试类似:

$count = 1;
echo "<tr>";
foreach($title_pieces as $tpiece){
    if ($count % NUM_COLS == 0)
         echo "</tr><tr>";

    echo "<td>$tpiece</td>";
    $count++;
}
echo "</tr>";

There's probably a clever optimization in there. What you are doing is counting the number of cells and starting a new row when the count divides NUM_COLS evenly. It's important count starts on 1, not 0, or you'll have an empty row.

于 2013-02-08T01:05:57.987 回答
0

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>';
?>
于 2013-02-08T01:07:16.800 回答