Question: I want to implement a logic for creating a three column table using foreach loop. A sample code will look like this. 

```
$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>';            

```

Any ideas?

Expected output is a simple 3X3 table with the values from the array

Answer: 
This should work for you:

(See that I added a `tr` at the start and end before and after the foreach loop. Also I changed the quotes to double quotes and made sure you append the text everywhere.)

```
<?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;

?>

```

output:

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

```
Here is an easy solution with incrementing that works with a dynamically generated table like the one in Magento product view page to help display product attributes in two table columns preceded by attribute label -> practically we have 4 table columns together with attribute labels. This is useful for products with multiple attributes that take a long page to display.

```
<?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>

```