我有一个包含23个对象的数组,我想循环并生成表单的复选框。使用Laravel,我提出了这个:
{{ Form::open(['url' => 'panel/update/games', 'id' => 'ajax']) }}
<div class="row">
<div class="col-sm-6">
@foreach($data['stats'] as $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() }}
效果很好;它会生成23个不同的复选框,但是,我想使用bootstrap 3的响应网格将这些结果分成两列。所以数据应如下所示:
<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>
print_r
上的$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] =>
)
)
答案 0 :(得分:4)
只需添加一个计数器变量,并在达到正确的数字时添加结束打开的分数:
<div class="row">
<div class="col-sm-6">
<?php $count=0;?>
@foreach($data['stats'] as $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>
或者使用for循环:
@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