在PHP中的foreach循环内的第一项之后添加条件

时间:2019-03-04 10:47:18

标签: php loops foreach conditional-statements

嗨,我有带foreach循环的这张桌子。我想在第一行之后添加一个条件。该行的每一端都有一个添加新行的按钮。我想做的就是更改该按钮,开始第二个循环。

这是代码

@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

有关如何解决此问题的任何建议?提前非常感谢您!

1 个答案:

答案 0 :(得分:2)

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