不显示删除的日期,并显示下一个可用的日期

时间:2018-12-18 09:59:30

标签: php laravel

我有一个网站,显示接下来的5天交货。这5天是明天,其后是4个工作日。

这样显示

$daysOff = ['Sat', 'Sun'];

        for ($days = 1; $days <= 7; $days++) {
            $day = date("D", strtotime("today + $days day"));
            if (!in_array($day, $daysOff)) {
                $daysOnRearranged[] = date("D, j M", strtotime("today + $days day"));
            }
        }

然后,我循环浏览并显示不同的定价信息。我不会在这里显示所有内容。

    foreach ($daysOnRearranged as $key => $day) {

        $product = Product::where('id', $order->product_id)->first();


        $product->date             = $day;
        $product->deal             = $deal;
        $product->units            = $order['units'];
        $product->amountExclVat    = $order->price->priceExclVat($amount);
        $product->amountVat        = $order->price->vatTotal($amount);
        $product->unitPriceInclVat = $unit_price;
        $product->unitPriceExclVat = $order->price->unitPriceExclVat($unit_price);
        $product->deliveryRate     = $deliveryRate;
        $product->vat              = $product['vat'];



        //Add product to collection

        $today = Carbon::parse($product->date)->format('Y-m-d');
        $nonDeliveryDays = Day::where('sale_at', $today)->where('is_not_delivery_day', 1)->first();


        if(is_null($nonDeliveryDays)){
          $products->push($product);
        }
    }

然后是视图

      @foreach($products as $key => $product)

        <div class="delivery-dates-tab @if ($product->deal === 1){{'is-deal'}}@endif">
          <input type="radio" value="{{ $key }}" name="delivery_date" id="date-price-tab-{{$loop->index}}" {{$product->deal === 1 ? 'checked' : ''}}>
          <label class="btn btn-primary not-active" for="date-price-tab-{{$loop->index}}">

            <p class="delivery-dates-tab__date">{{ date('D M d', strtotime($product->date)) }}</p>
            <p class="delivery-dates-tab__price">€{{ $product->amountInclVat }}</p>
            <small>{{ $product->units }} L</small>
            <details>
              <summary>Price Details</summary>
              <p><b>Price Per Litre:</b></p><br />
              <p>&euro;{{ $product->unitPriceInclVat }}<small> inc. VAT</small> </p><br />
              <p>&euro;{{ $product->unitPriceExclVat }}<small> ex. VAT</small></p><br />
              <p name="delivery" value={{ $product->deliveryRate }}><b>Delivery</b> &euro;{{ $product->deliveryRate }}</p><br />

              <br />
              <summary>Breakdown</summary>
              <p>Amount ex. VAT: &euro;{{ $product->amountExclVat}}</p>
              <p>Total VAT({{ $product->vat }}%): &euro;{{ $product->amountVat}} </p>
            </details>

          </label>
        </div>
      @endforeach

我可以使用nonDeliveryDays变量删除交货天数。但是,如果有2个匹配条件,则仅显示3天。如果删除了两天,该如何显示接下来的两天?

1 个答案:

答案 0 :(得分:1)

您可以将两个for循环组合成一个while,如下所示:

$products = [];
$days = 1;

//repeat until we have 5 products in the list
while (sizeof($products) < 5) {

    $day = date("D, j M", strtotime("today + $days day"));

    $product = Product::where('id', $order->product_id)->first();

    $product->date             = $day;
    $product->deal             = $deal;
    $product->units            = $order['units'];
    $product->amountExclVat    = $order->price->priceExclVat($amount);
    $product->amountVat        = $order->price->vatTotal($amount);
    $product->unitPriceInclVat = $unit_price;
    $product->unitPriceExclVat = $order->price->unitPriceExclVat($unit_price);
    $product->deliveryRate     = $deliveryRate;
    $product->vat              = $product['vat'];



    //Add product to collection

    $today = Carbon::parse($product->date)->format('Y-m-d');
    $nonDeliveryDays = Day::where('sale_at', $today)->where('is_not_delivery_day', 1)->first();


    if(is_null($nonDeliveryDays)){
      $products->push($product);
    }

    // go to the next day
    $days++;
}