2017-07-26 243 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