dongtuota3633
dongtuota3633
2017-05-05 06:16
浏览 10
已采纳

序列号没有增加

Here is my code,

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

When i execute this code, the serial number remains 1 for addition of more values.. For each and every value that has been added was with serial number 1.. How to get the increment of values like 2,3,4.. so on??

图片转代码服务由CSDN问答提供 功能建议

这是我的代码,

 &lt; tbody&gt; \  n&lt;?php $ j = 1;  ?&gt; 
 @foreach($ items as $ item)
&lt; tr&gt; 
&lt; td class =“text-right”&gt; {{$ j}}&lt; / td&gt; 
&lt; td&gt;  ; {{$ item-&gt; product_name}}&lt; / td&gt; 
&lt; td&gt; {{$ item-&gt; product_name2}}&lt; / td&gt; 
&lt; td&gt; {{$ item-&gt; 数量}}&lt; / td&gt; 
&lt; td class =“text-center”&gt;&lt; a href =“{{route('Conversion.edit',$ item-&gt; id)}}”class =  “btn btn-warning”&gt;&lt; span class =“glyphicon glyphicon-pencil”aria-hidden =“true”&gt;&lt; / span&gt;&lt; / a&gt;&lt; / td&gt; 
&lt; td class =  “text-center”&gt; 
 {!! {!!  Form :: open([
'method'=&gt;'DELETE',
'route'=&gt; ['Conversion.destroy',$ item-&gt; id] 
])!!} 
 \  n {!!  Form :: button('&lt; i class =“glyphicon glyphicon-remove”&gt;&lt; / i&gt;',array('type'=&gt;'submit','class'=&gt;'btn btn-danger'  ))}} 
 
 {!!  Form :: close()!!} 
&lt; / td&gt; 
&lt; / tr&gt; 
 @endforeach 
&lt;?php $ j ++;  ?&gt; 
&lt; / tbody&gt; 
   
 
 

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dpbf62565
    dpbf62565 2017-05-05 06:23
    已采纳

    while you are using foreach why use extra variable you can do that with foreach like that

    @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
    

    but if want to use your method then just simply put <?php $j++; ?> inside the foreach loop

    点赞 评论
  • douyuan4357
    douyuan4357 2017-05-05 06:18

    it's because your increment is after loop. place it in side foreach loop

     <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>
    
    点赞 评论
  • dsxd62219570
    dsxd62219570 2017-05-05 06:18

    Your increment counter must be within foreach loop.

    Write your lines as below:-

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

    OR you can do that without using counter

     <?php $items = array_values($items); 
        array_unshift($items,"");
        unset($items[0]);
     ?>
     @foreach($items as $k => $item)
    

    now use $k

    点赞 评论
  • duanhe1976
    duanhe1976 2017-05-05 06:26
    • Do it simple as

      @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>
      
    点赞 评论
  • doubi1910
    doubi1910 2017-05-05 06:29

    Your Code should be look like:

    <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>
    
    点赞 评论
  • doudiza9154
    doudiza9154 2017-05-05 09:16

    Simply try below code :

    @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
    
    点赞 评论

相关推荐