for循环不起作用

时间:2017-07-26 12:03:31

标签: php laravel

我有这个代码我想要始终从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 

3 个答案:

答案 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的值。