douqiao6015 2019-03-04 10:47
浏览 148
已采纳

在php中的foreach循环中的第一个项之后添加条件

Hi I have this table with foreach loop. I want to add a condition after the first row. every end of the row there's a button that adds a new row. What I want to do is to change that button starting the second loop.

Here's the code

@foreach($order->orderItems as $orderItem)
<tr>
  <td><input class="form-control autocomplete_txt" type='text' data-type="product_code" id='product_code_{{ $orderItem->id }}' name='product_code[]' for="1" value="{{ $orderItem->product_code }}" required/></td>
  <td><input class="form-control autocomplete_txt" type='text' data-type="product_name" id='product_name_{{ $orderItem->id }}' name='product_name[]' for="1" value="{{ $orderItem->product_name }}" required/></td>
  <td><input class="form-control product_price" type='number' data-type="product_price" id='product_price_{{ $orderItem->id }}' name='cost[]' for="1" value="{{ $orderItem->cost }}" required/></td> <!-- purchase_cost -->
  <td><input class="form-control quantity" type='number' id='quantity_{{ $orderItem->id }}' name='quantity[]' for="1" value="{{ $orderItem->quantity }}" required/></td>
  <td><input class="form-control total_cost" type='text' id='total_cost_{{ $orderItem->id }}' name='total_cost[]' for='1' value="{{ number_format($orderItem->total_cost, 2) }}" readonly/>
  <input class="form-control product_id" type='hidden' data-type="product_id" id='product_id_{{ $orderItem->id }}' name='product_id[]'/>
  <input class="form-control product_id" type='hidden' data-type="order_id" id='oder_id_{{ $orderItem->id }}' name='order_id[]' value="1" /></td>
  <td>
  @if ($orderItem % 3 == 0) {
    <button type="button" name="add" id="add" class="btn btn-success circle"><i class="fas fa-plus-circle"></i></button>
  @endif
  </td>
</tr>
@endforeach

any suggestions on how to fix this? thank you so much in advance!

  • 写回答

1条回答 默认 最新

  • douzao1119 2019-03-04 10:55
    关注
    // add counter in the loop. when loop count is greater than 1 button will be shown.
        $counter = 0;
        @foreach($order->orderItems as $orderItem)
        {
            $counter++; 
            <tr>
              <td><input class="form-control autocomplete_txt" type='text' data-type="product_code" id='product_code_{{ $orderItem->id }}' name='product_code[]' for="1" value="{{ $orderItem->product_code }}" required/></td>
              <td><input class="form-control autocomplete_txt" type='text' data-type="product_name" id='product_name_{{ $orderItem->id }}' name='product_name[]' for="1" value="{{ $orderItem->product_name }}" required/></td>
              <td><input class="form-control product_price" type='number' data-type="product_price" id='product_price_{{ $orderItem->id }}' name='cost[]' for="1" value="{{ $orderItem->cost }}" required/></td> <!-- purchase_cost -->
              <td><input class="form-control quantity" type='number' id='quantity_{{ $orderItem->id }}' name='quantity[]' for="1" value="{{ $orderItem->quantity }}" required/></td>
              <td><input class="form-control total_cost" type='text' id='total_cost_{{ $orderItem->id }}' name='total_cost[]' for='1' value="{{ number_format($orderItem->total_cost, 2) }}" readonly/>
              <input class="form-control product_id" type='hidden' data-type="product_id" id='product_id_{{ $orderItem->id }}' name='product_id[]'/>
              <input class="form-control product_id" type='hidden' data-type="order_id" id='oder_id_{{ $orderItem->id }}' name='order_id[]' value="1" /></td>
              <td>
              @if ($orderItem % 3 == 0 && $counter > 1) {
                <button type="button" name="add" id="add" class="btn btn-success circle"><i class="fas fa-plus-circle"></i></button>
              @endif
              </td>
            </tr>
        }
        @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵