ttmt_73 Posted June 16, 2015 Share Posted June 16, 2015 (edited) I need to create structure like this <div class="row"> <div class="col-sm-3"> <!--content loop 1--> </div> <div class="col-sm-3"> <!--content loop 1--> </div> <div class="col-sm-3"> <!--content loop 2--> </div> <div class="col-sm-3"> <!--content loop 2--> </div> </div> <div class="row"> <div class="col-sm-3"> <!--content loop 3--> </div> <div class="col-sm-3"> <!--content loop 3--> </div> <div class="col-sm-3"> <!--content loop 4--> </div> <div class="col-sm-3"> <!--content loop 4--> </div> </div> I need to create it in a single foreach loop but on each loop I only create this structure. <div class="col-sm-3"> <!--content loop 1--> </div> <div class="col-sm-3"> <!--content loop 1--> </div> I thought I had it working with this. But this seems to skip the 3rd element in the array. <div class="row"> <?php $i = 0; foreach(....../*your code*/){ if($i == 2){ $i = 0; ?> </div><div class="row"> <?php } else { ?> <div class="col-sm-3"> <!--content--> </div> <?php $i++; } } ?> </div> I have a demo here to illustrate - http://viper-7.com/EsGoc9 Edited June 16, 2015 by ttmt_73 Quote Link to comment https://forums.phpfreaks.com/topic/296867-create-html-structure-in-foreach-loop/ Share on other sites More sharing options...
requinix Posted June 16, 2015 Share Posted June 16, 2015 (edited) What are the various "content loop" numbers? Sounds like you basically just want a four-column layout, except using Bootstrap tables instead of actual tables. (Speaking of, are you sure you should not be using an actual table?) column = 1 loop { if column = 1 then output column start output cell column = column % 4 + 1 if column = 1 then output column end } // normally you would close out incomplete rows here // no need with bootstrap if column != 1 then output column end $column = 1; foreach (/* whatever */) { if ($column == 1) { echo "<div class='row'>"; } echo "<div class='col-sm-3'>"; // whatever echo "</div>"; $column = ($column % 4) + 1; if ($column == 1) { echo "</div>"; } } if ($column != 1) { echo "</div>"; }But maybe I didn't understand your question. Maybe you want to use two loops at once? Edited June 16, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/296867-create-html-structure-in-foreach-loop/#findComment-1514126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.