0

PHP でテーブルを生成するのに助けが必要です。

10 -15 エントリの配列があります。このような:

$array = array(IME1, IME2, IME3, IME4,IME5, IME6, IME7 ,IME8 ,IME9 ,IME10);

目標は、次のようなテーブルを作成することです: 3 番目のエントリごとに自動的に新しい行が作成されます。

例

誰かがこれを手伝ってくれますか?

4

2 に答える 2

4

の仕事のようですarray_chunk()

array_chunk($array, 3);
于 2012-05-05T15:51:03.377 に答える
0

Not the prettiest solution

$chunks = array_chunk($array, $max_col); 
$max_col = 3; 
?>
<table border="1">
    <tbody>
        <? foreach($chunks as $key => $value){
            echo "<tr>";
            foreach($value as $k => $v){
                echo "<td> ".$v." </td>";
            }
            // Finish tr, but only if there are no items left for the next row
            if( count($value) == $max_col ){
                echo "</tr>";
            }
        }
        // Finish td with an empty value setting colspan if there are not enough items to fil the entire row
        if( count($value) < $max_col ){
            echo "<td colspan=".($max_col-count($v)-1)."></td>";
            // Finish the last row
            echo "</tr>";
        }
        ?>
    </tbody>
</table>

Think about separating your logic from your presentation. There are numerous template engines out there that would solve this problem in a breeze or let you define a function/modifier/snippet that takes care of this task and let you easily reuse it.

于 2012-06-14T23:41:08.643 に答える