简体   繁体   中英

Don't display removed days, and display next available days

I have a site, which displays the next 5 delivery days. The 5 days are tomorrow and the following 4 working days.

That's displayed like so

$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"));
            }
        }

I then loop through and display different pricing information. I wont show all of it here.

    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);
        }
    }

This is the view then

      @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

I can remove delivery days using the nonDeliveryDays variable. But if there are 2 matching criteria, there are only 3 days showing. How do I show the next 2 available days if two are removed?

You can combine two for loops into one while , like so:

$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++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM