PHP Foreach: 2 columns, 1 row, repeatedly
<p>I am trying to <strong>produce this</strong> with php foreach loop:</p>

<pre><code>&lt;form&gt;
   &lt;div class="form-group"&gt;

     &lt;div class="row"&gt;

        &lt;div class="col-md-6"&gt;
            DATA
        &lt;/div&gt;

        &lt;div class="col-md-6"&gt;
            DATA
        &lt;/div&gt;

     &lt;/div&gt;
   &lt;/div&gt;
&lt;/form&gt;
</code></pre>

<p><strong>Current</strong> PHP Code:</p>

<pre><code>$form = '';

$form .= '&lt;form&gt;';

$count = 0;
foreach ($fields as $f) {

   // open .form-group and .row every two loops?
   if($count % 2) $form .='&lt;div class="form-group"&gt;&lt;div class="row"&gt;';
   $form .= '&lt;div class="col-sm-6"&gt;'; // always open column

   $form .= 'Whatever data';

   $form .= '&lt;/div&gt;'; // always close column

   // close .form-group and .row every two loops?
   if($count % 2) $form .= '&lt;/div&gt;&lt;/div&gt;';

   $count++;
}


$form .= '&lt;/form&gt;';
</code></pre>

<p>I tried many possible ways, this is just an example of my code.</p>

<p>The point is to start the <code>.form-group</code> and <code>.row</code> and then loop twice the columns without the wrappers, if that makes any sense.</p>

<p>If I use <code>if ($count % 2 == 0)</code> instead, I get this <a href="http://i.imgur.com/AUvzjlw.png" rel="nofollow">http://i.imgur.com/AUvzjlw.png</a></p>

<p>A</p>