for loop - PHP Foreach: 2 columns, 1 row, repeatedly -


i trying produce this php foreach loop:

<form>    <div class="form-group">       <div class="row">          <div class="col-md-6">             data         </div>          <div class="col-md-6">             data         </div>       </div>    </div> </form> 

current php code:

$form = '';  $form .= '<form>';  $count = 0; foreach ($fields $f) {     // open .form-group , .row every 2 loops?    if($count % 2) $form .='<div class="form-group"><div class="row">';    $form .= '<div class="col-sm-6">'; // open column     $form .= 'whatever data';     $form .= '</div>'; // close column     // close .form-group , .row every 2 loops?    if($count % 2) $form .= '</div></div>';     $count++; }   $form .= '</form>'; 

i tried many possible ways, example of code.

the point start .form-group , .row , loop twice columns without wrappers, if makes sense.

if use if ($count % 2 == 0) instead, http://i.imgur.com/auvzjlw.png

a

change first

if ($count % 2) 

to

if ($count % 2 == 0) 

change second 1 to:

if ($count % 2 != 0) 

(this second change isn't strictly necessary, think makes code clearer.)

you want start new div when $count even, , end when $count odd. $count % 2 0 when it's even, , that's falsey.

demo

note if there can odd number of items in $fields, you'll need check after loop close last div. @ end of loop, do:

if ($count % 2 != 0) {     // close last div if there odd number of fields     $form .= '</div></div>'; } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -