php - Divide a foreach loops of 23 entries into two divs -
i have array 23 objects in them loop through , generate checkboxes form. using laravel, came this:
{{ form::open(['url' => 'panel/update/games', 'id' => 'ajax']) }} <div class="row"> <div class="col-sm-6"> @foreach($data['stats'] $stats => $stat) {{ form::checkbox('stats', $stat->field) }} {{ $stat->field }} <br> @endforeach </div> </div> {{ form::button('update statistics', ['type' => 'submit', 'class' => 'btn btn-info btn-block', 'data-after' => 'updated statistics|check']) }} {{ form::close() }}
this works well; generates 23 different checkboxes however, divide these results 2 columns using bootstrap 3's responsive grid. data should this:
<div class="row"> <div class="col-sm-6> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> </div> <div class="col-sm-6> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> <input type="checkbox" value="" name=""> </div> </div>
a print_r
on $data['stats']
:
array ( [0] => stdclass object ( [field] => username [type] => varchar(30) [null] => no [key] => pri [default] => [extra] => ) [1] => stdclass object ( [field] => prestige [type] => int(2) [null] => no [key] => [default] => 1 [extra] => ) [2] => stdclass object ( [field] => level [type] => int(3) [null] => no [key] => [default] => 1 [extra] => ) [3] => stdclass object ( [field] => experience [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [4] => stdclass object ( [field] => points [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [5] => stdclass object ( [field] => kills [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [6] => stdclass object ( [field] => deaths [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [7] => stdclass object ( [field] => teamwins [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [8] => stdclass object ( [field] => teamlosses [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [9] => stdclass object ( [field] => bonus [type] => float [null] => no [key] => [default] => 1 [extra] => ) [10] => stdclass object ( [field] => achievementscore [type] => int(10) [null] => no [key] => [default] => 0 [extra] => ) [11] => stdclass object ( [field] => demented [type] => int(1) [null] => no [key] => [default] => 1 [extra] => ) [12] => stdclass object ( [field] => volatile [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [13] => stdclass object ( [field] => undead [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [14] => stdclass object ( [field] => scavenger [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [15] => stdclass object ( [field] => divinity [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [16] => stdclass object ( [field] => withered [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [17] => stdclass object ( [field] => killswitch [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [18] => stdclass object ( [field] => precise [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [19] => stdclass object ( [field] => adept [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [20] => stdclass object ( [field] => utility [type] => int(1) [null] => no [key] => [default] => 0 [extra] => ) [21] => stdclass object ( [field] => class [type] => varchar(15) [null] => no [key] => [default] => warrior [extra] => ) [22] => stdclass object ( [field] => perk [type] => varchar(15) [null] => no [key] => [default] => demented [extra] => )
)
simply add counter variable , add closing opening div tages when reaches correct number:
<div class="row"> <div class="col-sm-6"> <?php $count=0;?> @foreach($data['stats'] $stats => $stat) {{ form::checkbox('stats', $stat->field) }} {{ $stat->field }} <br> <?php if ($count == 11):?> </div> <div class="col-sm-6"> <?php endif; $count++;?> @endforeach </div> </div>
or use loop:
@for ($i = 0; $i < count($data['stats']); $i ++) {{ form::checkbox('stats', $data['stats'][$i]->field) }} {{ $data['stats'][$i]->field }} <br> <?php if ($i == 11):?> </div> <div class="col-sm-6"> <?php endif;?> @endfor
Comments
Post a Comment