我有这个代码我想要始终从1开始,但它不能继续5 6 7 8等等。我希望value = {{k}}
始终从1
<?php $k=1; ?>
@foreach($answers as $answer)
@if($answer->deleted == 0)
<div class="form-group {{ $errors->has('options.'.$k) ? ' has-error': '' }}"
id="option{{ $k}}">
<div class="checkbox col-xs-2 control-label" id="checkbox_control-label">
<label>
<input type="text" name="order[{{ $k }}]" value="{{$k}}" id="input_order">
</label>
</div>
<div class="col-xs-8">
<input type="text" name="options[{{ $k }}]" value="{{ $answer->text}}"
class="form-control">
<div class="col-xs-12"></div>
<span class="help-block">
<strong></strong>
</span>
</div>
<div class="col-xs-2">
<button type="button" value="{{ $k }}" class="btn btn-flat btn-default btn-sm"
id="delete_option" title="@lang('general.remove_option')">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button>
</div>
</div>
@endif
<?php $k++; ?>
@endforeach
答案 0 :(得分:6)
只需在if条件结束前递增变量:
<?php $k++; ?>
@endif
@endforeach
答案 1 :(得分:0)
$ k ++里面如果条件.....
<?php $k=1; ?>
@foreach($answers as $answer)
@if($answer->deleted == 0)
<div class="form-group {{ $errors->has('options.'.$k) ? ' has-error': '' }}"
id="option{{ $k}}">
<div class="checkbox col-xs-2 control-label" id="checkbox_control-label">
<label>
<input type="text" name="order[{{ $k }}]" value="{{$k}}" id="input_order">
</label>
</div>
<div class="col-xs-8">
<input type="text" name="options[{{ $k }}]" value="{{ $answer->text}}"
class="form-control">
<div class="col-xs-12"></div>
<span class="help-block">
<strong></strong>
</span>
</div>
<div class="col-xs-2">
<button type="button" value="{{ $k }}" class="btn btn-flat btn-default btn-sm"
id="delete_option" title="@lang('general.remove_option')">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button>
</div>
</div>
<?php $k++; ?>
@endif
@endforeach
答案 2 :(得分:0)
只需在foreach的开头添加分配给zsh
$k
的值。