【问题标题】:increment value of i is not working properlyi 的增量值无法正常工作
【发布时间】:2020-10-08 23:17:25
【问题描述】:

我无法理解为什么 i 的值没有设置为零并且它的行为就像这样 st 的两个表单值的开头都设置为零但没有以 html 表单获得所需的输出

<div class="col-lg-6">
   <form name="assignNone" action="" method="post">
    @csrf
      @method('PUT')
       <?php $i=0 ?>
         @foreach($users as $user)
           @if($user->name=='NONE' )
             <input type="text" name="marks[{{$i}}]" value="">
           @endif
           <?php $i++; ?>
        @endforeach
    </form>
</div>
<div class="col-lg-6">
  <form name="assignabc" action="" method="post">
    @csrf
      @method('PUT')
        @if(!empty($iprnNumbers))
            <?php $i=0 ?>
                @foreach($users as $user )
                    @if($user ->name!='NONE' )
                        <input type="text" name="marks[{{$i}}]" value="">
                    @endif
                <?php $i++; ?>
            @endforeach
        @endif
    </form>
</div>

输出显示为

<div class="col-lg-6">
       <form name="assignNone" action="" method="post">
          <input type="text" name="marks[2] value="">
          <input type="text" name="marks[3] value="">
          <input type="text" name="marks[4] value="">
        </form>
    </div>
    <div class="col-lg-6">
      <form name="assignabc" action="" method="post">
        <input type="text" name="marks[0]" value="">
        <input type="text" name="marks[1]" value="">
       </form>
    </div>

我想要的结果应该是这样的

<div class="col-lg-6">
       <form name="assignNone" action="" method="post">
          <input type="text" name="marks[0] value="">
          <input type="text" name="marks[1] value="">
          <input type="text" name="marks[2] value="">
        </form>
    </div>
    <div class="col-lg-6">
      <form name="assignabc" action="" method="post">
        <input type="text" name="marks[0]" value="">
        <input type="text" name="marks[1]" value="">
       </form>
    </div>

【问题讨论】:

  • &lt;?php $i++; ?&gt; 放入@if(...) @endif
  • Misspled name="marks[0] 应该是 name="marks[0]" quatation 错误

标签: php html laravel laravel-blade


【解决方案1】:

下面有一个叫做动态输入的简短示例:

<form name="assignNone" action="" method="post">   
  <input type="text" name="marks[]" value="one">
  <input type="text" name="marks[]" value="two">
  <input type="text" name="marks[]" value="three">
</form>

然后在您的控制器中,您将能够像这样获得所有marks 值:

$marks = $request->marks; // give you an array with values

这将很容易解决您的问题,因为它易于实施且易于理解!

【讨论】:

    【解决方案2】:

    首先应该是&lt;?php $i = 0; ?&gt;。注意分号。其次,您需要在 if 条件内移动增量,所以它看起来像 -

     <?php $i++; ?>
    @endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多