2017-07-26 125 views
2

我有这个代码我想始终从1开始,但它不工作,它继续5 6 7 8等等。我想要的值= {{k}}总是从1for循环不工作

<?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 
+1

在@ endif前移动增量变量'if'块内部的变量'$ k ++' – Thamilan

+1

$ K ++如果条件满足 – Abid

回答

6

开始,如果条件结束之前就增加变量:

 <?php $k++; ?> 
    @endif 
@endforeach 
0

$ķ++里面,如果条件.....

 <?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 
0

只要在该foreach的开头将值分配给$k这将是1