序列号没有增加

时间:2017-05-05 06:16:55

标签: php laravel laravel-5

这是我的代码,

  <tbody>
    <?php $j = 1; ?>
    @foreach($items as $item)
    <tr>
      <td class="text-right">{{ $j }}</td>
      <td>{{ $item->product_name }}</td>
      <td>{{ $item->product_name2 }}</td>
      <td>{{ $item-> quantity}}</td>
      <td class="text-center"><a href="{{ route('Conversion.edit', $item->id) }}" class="btn btn-warning"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
      <td class="text-center">
        {!! Form::open([
          'method' => 'DELETE',
          'route' => ['Conversion.destroy', $item->id]
          ]) !!}

          {!! Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => 'btn btn-danger')) !!}

          {!! Form::close() !!}
        </td>
      </tr>
      @endforeach
      <?php $j++; ?>
    </tbody>

当我执行此代码时,序列号保持为1以添加更多值。对于每个已添加的值,序列号为1 ..如何获取值增量,如2,3,4 ..等等?

6 个答案:

答案 0 :(得分:2)

您的代码应如下所示:

<tbody>
<?php $j = 1; ?>
@foreach($items as $item)
<?php $j++; ?>
<tr>
  <td class="text-right">{{ $j }}</td>
  <td>{{ $item->product_name }}</td>
  <td>{{ $item->product_name2 }}</td>
  <td>{{ $item-> quantity}}</td>
  <td class="text-center"><a href="{{ route('Conversion.edit', $item->id) }}" class="btn btn-warning"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
  <td class="text-center">
    {!! Form::open([
      'method' => 'DELETE',
      'route' => ['Conversion.destroy', $item->id]
      ]) !!}

      {!! Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => 'btn btn-danger')) !!}

      {!! Form::close() !!}
    </td>
  </tr>
  @endforeach
</tbody>

答案 1 :(得分:1)

这是因为你的增量是在循环之后。将它放在foreach循环

 <tbody>
    <?php $j = 1; ?>
    @foreach($items as $item)
    <tr>
      <td class="text-right">{{ $j }}</td>
      <td>{{ $item->product_name }}</td>
      <td>{{ $item->product_name2 }}</td>
      <td>{{ $item-> quantity}}</td>
      <td class="text-center"><a href="{{ route('Conversion.edit', $item->id) }}" class="btn btn-warning"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
      <td class="text-center">
        {!! Form::open([
          'method' => 'DELETE',
          'route' => ['Conversion.destroy', $item->id]
          ]) !!}

          {!! Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => 'btn btn-danger')) !!}

          {!! Form::close() !!}
        </td>
      </tr>
        <?php $j++; ?>
      @endforeach

    </tbody>

答案 2 :(得分:1)

您的增量计数器必须在foreach循环中。

写下你的行: -

<?php $j++; ?>
@endforeach

或者你可以不使用计数器

来做到这一点
 <?php $items = array_values($items); 
    array_unshift($items,"");
    unset($items[0]);
 ?>
 @foreach($items as $k => $item)

现在使用$k

答案 3 :(得分:1)

当你使用foreach时为什么要使用额外的变量你可以用foreach这样做

@foreach($items as $key =>$item)                    
    <tr>
      <td class="text-right"> {{$key+1}} </td>
      <td>{{ $item->product_name }}</td>
      <td>{{ $item->product_name2 }}</td>
      <td>{{ $item-> quantity}}</td>
      <td class="text-center"><a href="{{ route('Conversion.edit', $item->id) }}" class="btn btn-warning"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
      <td class="text-center">
        {!! Form::open([
          'method' => 'DELETE',
          'route' => ['Conversion.destroy', $item->id]
          ]) !!}

          {!! Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => 'btn btn-danger')) !!}

          {!! Form::close() !!}
        </td>
      </tr>
      @endforeach

但是如果想要使用你的方法,那么只需将<?php $j++; ?>放在foreach循环中

即可

答案 4 :(得分:1)

  • 按照

    进行操作

    @foreach($items as $key=>$item)
    <tr>
      <td class="text-right">{{ $key+1 }}</td>
      <td>{{ $item->product_name }}</td>
      <td>{{ $item->product_name2 }}</td>
      <td>{{ $item-> quantity}}</td>
      <td class="text-center"><a href="{{ route('Conversion.edit', $item->id) }}" class="btn btn-warning"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
      <td class="text-center">
        {!! Form::open([
          'method' => 'DELETE',
          'route' => ['Conversion.destroy', $item->id]
          ]) !!}
    
          {!! Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => 'btn btn-danger')) !!}
    
          {!! Form::close() !!}
        </td>
      </tr>
      @endforeach
    
    </tbody>
    

答案 5 :(得分:1)

只需尝试以下代码:

@foreach($items as $key =>$item)                    
    <tr>
      <td class="text-right"> {{$key+1}} </td>
      <td>{{ $item->product_name }}</td>
      <td>{{ $item->product_name2 }}</td>
      <td>{{ $item-> quantity}}</td>
      <td class="text-center"><a href="{{ route('Conversion.edit', $item->id) }}" class="btn btn-warning"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
      <td class="text-center">
        {!! Form::open([
          'method' => 'DELETE',
          'route' => ['Conversion.destroy', $item->id]
          ]) !!}

          {!! Form::button('<i class="glyphicon glyphicon-remove"></i>', array('type' => 'submit', 'class' => 'btn btn-danger')) !!}

          {!! Form::close() !!}
        </td>
      </tr>
      @endforeach