2012-05-05 22 views
0

我需要一些帮助,用PHP生成一个表格。我有一个10 -15项的数组。 像这样:如何自动创建一个新的表格行

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

的目标是创建这样一个表:在每3项自动新行。

example

有人能帮助我,好吗?

回答

4

看起来像一个工作,array_chunk()

array_chunk($array, 3); 
+0

我需要一些帮助如何使表工作? – Axxess

0

不是最漂亮的解决方案

$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> 

想想从您的演示文稿分离你的逻辑。有很多模板引擎可以轻松解决这个问题,或者让您定义一个处理此任务的函数/修饰符/代码段,并让您轻松地重用它。

相关问题