我有一个网站,显示接下来的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>€{{ $product->unitPriceInclVat }}<small> inc. VAT</small> </p><br />
<p>€{{ $product->unitPriceExclVat }}<small> ex. VAT</small></p><br />
<p name="delivery" value={{ $product->deliveryRate }}><b>Delivery</b> €{{ $product->deliveryRate }}</p><br />
<br />
<summary>Breakdown</summary>
<p>Amount ex. VAT: €{{ $product->amountExclVat}}</p>
<p>Total VAT({{ $product->vat }}%): €{{ $product->amountVat}} </p>
</details>
</label>
</div>
@endforeach
我可以使用nonDeliveryDays变量删除交货天数。但是,如果有2个匹配条件,则仅显示3天。如果删除了两天,该如何显示接下来的两天?
答案 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++;
}