使用 foreach 循环实现多列 table

Implement a multiple column table using foreach loop

我想实现一个使用 foreach 循环创建三列 table 的逻辑。示例代码如下所示。

$array = ['0','1','2','3','4','5','6'];
$table = '<table class="table"><tbody>';

foreach($array as $a=>$v){

    //if 0th or 3rd???????????wht should be here?
    $table .= '<tr>';

    $table .= '<td>$v</td>';

    //if 2nd or 5th??????????and here too???
    $table .= '</tr>';

}

$table = '</tbody></table>';            

有什么想法吗?

预期输出是一个简单的 3X3 table,其值来自数组

使用这个你可能正在寻找这个

<?php
echo('<table><tr>');
$i = 0;
foreach( $array as $product )
{
   $i++;
   echo '<td>'.$product .'</td>';
   if($i % 3==0)
   {
      echo '</tr><tr>';
   }
}
echo'</tr></table>';
?>

结果在这里:

<table>
   <tr>
      <td>data1</td>
      <td>data2</td>
      <td>data3</td>    
   </tr>    
   <tr>
     <td>data4</td>
     <td>data5</td>
     <td>data6</td>    
   </tr>
</table>

这应该适合你:

(看到我在 foreach 循环之前和之后的开始和结束处添加了一个 tr。此外,我将引号更改为双引号,并确保在任何地方都附加了文本。)

<?php

    $array = ['0','1','2','3','4','5','6'];
    $table = "<table class='table'><tbody><tr>";
                                        //^^^^ See here the start of the first row
    foreach($array as $a => $v) {

        $table .= "<td>$v</td>";
                //^           ^ double quotes for the variables
        if(($a+1) % 3 == 0)
            $table .= "</tr><tr>";

    }
    $table .= "</tr></tbody></table>"; 
         //^   ^^^^^ end the row
         //| append the text and don't overwrite it at the end
    echo $table;

?>

输出:

<table class='table'>
<tbody>
    <tr>
        <td>0</td>
        <td>1</td>
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
        <td>4</td>
        <td>5</td>
    </tr>
    <tr>
        <td>6</td>
    </tr>
</tbody>
</table>

这里有一个使用 array_chunk() 的简单解决方案:

<?php

    $array = array('0','1','2','3','4','5','6');
    $d = array_chunk($array, 3);

    $table = "<table border='1' class='table'><tbody>";
    foreach($d as $v)
        $table .= "<tr><td>" . implode("</td><td>", $v) . "</td></tr>";

    $table .= "</tbody></table>";
    echo $table;

?>

这是一个简单的递增解决方案,它与动态生成的 table 一起使用,例如 Magento 产品视图页面中的那个,有助于在属性标签前面的两个 table 列中显示产品属性 ->实际上我们有 4 table 列和属性标签。这对于需要很长的页面才能显示的具有多个属性的产品很有用。

<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
if($_additional = $this->getAdditionalData()): ?>

<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="table table-bordered table-hover" id="product-attribute-specs-table">
    <col width="25%" />
    <col />
    <tbody>
    <?php $i = 1; ?>
    <tr>
    <?php foreach ($_additional as $_data): ?>
    <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>


            <th style="width: 20%;"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
            <td class="data" style="width:20%;"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
            <?php $i++; if($i > 1 && ($i & 1) ) echo "</tr><tr>";?>

    <?php } ?>
    <?php endforeach; ?>
    </tr>
    </tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>