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>

```
Use this you may looking for this 

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

```

**Result Here:**

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

```