首页 > 解决方案 > 如何打破第二个foreach循环

问题描述

实际上,在这段代码中,我试图显示form labelandform input字段来自数据库是动态的。

如果第二个 foreach 没有数据,两个 foreach 都不起作用我该怎么办?

@section('body') 

     <!--{{session('uid')}} -->

      <div class="col-sm-12">
        <!--      Wizard container        -->
             <div class="wizard-container">
                <div class="card wizard-card" data-color="red" id="wizard">
                   <form action="{{URL::to('faculty/submitreport')}}" method="post" name="reportform">
                       <!--        You can switch " data-color="blue" "  with one of the next bright colors: "green", "orange", "red", "purple"             -->
                        {{ csrf_field()}}
                        <div class="wizard-header">
                           <h3 class="wizard-title">
                               Monthly Progress Report
                           </h3>
                           <h5>This information will let us know more about you.</h5>
                         </div>

                    @foreach($fetchform as $data)
                      @foreach($fetchans as $ans)

                        @if(is_null($ans)){
                           @break

                           <div class="wizard-navigation">
                              <ul>
                                <li><a href="#details" data-toggle="tab">{{$data->f_title}}</a></li>
                              </ul>
                           </div>

                           <div class="tab-content">
                              <div class="tab-pane" id="details">
                                 <div class="row">                  
                                    <div class="col-lg-12">
                                       @if(is_null($data->f1))
                                       @else

                                        <div class="form-group label-floating">                                    <label class="control-label">{{$data->f1}}</label>
                                             <input name="f1" type="text" class="form-control"value='{{$ans->f1}}'></input>     
                                        </div>
                                       @endif                                       

                            @if(is_null($data->f2))
                            @else
                                <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f2}}</label>
                                    <input name="f2" type="text" class="form-control" value='{{$ans->f2}}'></input>     
                                </div>
                            @endif          

                            @if(is_null($data->f3))
                            @else
                                <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f3}}</label>
                                    <input name="f3" type="text" class="form-control" value='{{$ans->f3}}'></input>     
                                </div>
                            @endif          

                            @if(is_null($data->f4))
                            @else
                              <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f4}}</label>
                                    <input name="f4" type="text" class="form-control" value='{{$ans->f4}}'></input>     
                               </div>
                            @endif          

                            @if(is_null($data->f5))
                            @else
                               <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f5}}</label>
                                    <input name="f5" type="text" class="form-control" value='{{$ans->f5}}'></input>     
                               </div>
                            @endif          

                            @if(is_null($data->f6))
                            @else
                               <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f6}}</label>
                                    <input name="f6" type="text" class="form-control" value='{{$ans->f6}}'></input>     
                               </div>
                            @endif          

                            @if(is_null($data->f7))
                            @else
                               <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f7}}</label>
                                    <input name="f7" type="text" class="form-control" value='{{$ans->f7}}'></input>     
                               </div>
                            @endif          

                            @if(is_null($data->f8))
                            @else
                               <div class="form-group label-floating">
                                    <label class="control-label">{{$data->f8}}</label>
                                    <input name="f8" type="text" class="form-control" value='{{$ans->f8}}'></input>     
                               </div>
                            @endif          

                            @if(is_null($data->f9))
                            @else
                               <div class="form-group label-floating">
                                <b> <label class="control-label">{{$data->f9}}</label></b>
                                    <input name="f9" type="text" class="form-control" value='{{$ans->f9}}'></input>     
                               </div>
                           @endif           

                           @if(is_null($data->f10))
                           @else
                               <div class="form-group label-floating">
                                 <b>    <label class="control-label">{{$data->f10}}</label></b>
                                    <input name="f10" type="text" class="form-control" value='{{$ans->f10}}'></input>       
                               </div>
                           @endif           

                           @if(is_null($data->f11))
                           @else
                               <div class="form-group label-floating">
                                  <b>       <label class="control-label">{{$data->f11}}</label> </b>
                                    <input name="f11" type="text" class="form-control" value='{{$ans->f11}}'></input>       
                               </div>
                           @endif           

                           @if(is_null($data->f12))
                           @else
                              <div class="form-group label-floating">
                                 <b>    <label class="control-label">{{$data->f12}}</label></b>
                                    <input name="f12" type="text" class="form-control" value='{{$ans->f12}}'></input>       
                              </div>
                           @endif

                           <input type='hidden' name="f_id" value='{{$data->f_id}}'>
                           <input type='hidden' name="u_id" value='{{session()->get("uid")}}'>
                           <input type='hidden' name="f_status" id="f_status" />
                    </div>
                   }
                 @endif                                 
              @endforeach
            @endforeach  

         </div>
        </div>
       </div>       
     </div>

     <div class="wizard-footer">
          <div class="pull-right">
              <input type='submit' class='btn btn-next btn-fill btn-danger btn-wd-lg' name='save' value='Save' />
               <input type='button' class='btn btn-finish btn-fill btn-danger btn-wd-lg' onclick="myFunction()" name='Submit' value='submit' />
          </div>
          <div class="clearfix"></div>
        </div>
      </form>
    </div>
 </div> <!-- wizard container -->
</div>

<script>
function myFunction() {
    var txt;
    var r = confirm("Really Do You Want To Finally Submit a Form \n After Submitting You Can't Update Anything into It.");
    if (r == true) {
        document.getElementById("f_status").value = '1';
        reportform.submit();
    } else {

    }

}
</script>

@endsection

标签: laravellaravel-5.6

解决方案


通过阅读您的代码,您似乎正在尝试用变量的值填充label元素,并用$data变量的值填充input字段$ans。由于现在编写代码,您正在循环遍历所有项目,$fetchform并且每次循环时都会遍历$fetchans. 因此,如果您有 3 个表单项和 3 个答案,您实际上会运行 9 个循环。您将获得所有可能的值组合。

似乎每个fetchform都有相应的fetchans. 如果是这种情况,继续上面的示例,您只需要运行 3 个循环。如果我要编写这段代码,我会让它看起来像这样:

    //loop through all items in $fetchform
    @for ($i = 0; $i < count($fetchform); $i++)
        //php tags needed in blade to set the variables
        @php 
            $data = $fetchform[$i]; //get the form data
            $ans = $fetchans[$i]; //get the corresponding form answer
        @endphp

        //it might be more appropriate to check these for 'empty' instead of null
        //that all depends on your code
        @if(!is_null($data) || !is_null($ans))

            //make sure object has the 'f1' key and value is not null
            @if(array_key_exists('f1', $data) && !is_null($data->f1))
                <div class="form-group label-floating">
                    <label class="control-label">{{ $data->f2 }}</label>
                    <input name="f2" type="text" class="form-control" value='{{ $ans->f2 }}' />     
                </div>
            @endif
        @endif
    @endfor

请记住,上面的代码对您的问题做了一些假设。首先,它假定$fetchform$fetchans是可以通过值访问的数值数组$i。其次,它假设$fetchform$fetchans具有对应的键值,因此数据和答案在各自的数组中都是相同的数字键。最后,它假设这些值在不存在时将是null与being empty,我认为更可能是这种情况。您可能需要修改我提供的代码以满足您的确切需求。玩它,看看它是否有效。


推荐阅读