Skip to content
Advertisement

increment value of i is not working properly

I am not able to understand why value of i is not setting to zero and it’s behaving like this st the start of both form value of i is set to zero but not getting the desired out in html form

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

output is showing as

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

result i want should be something like

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

Advertisement

Answer

First it should be <?php $i = 0; ?>. Notice the semicolon. Second, you need to move the increment inside the if condition, so it would look like-

 <?php $i++; ?>
@endif
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement