php - 如果在表中找不到行,则使用连字符 (-) 显示数据 - php
问题描述
我不知道任何一个标题都与问题完全匹配,但这是我的场景。我正在尝试在表格中显示溜冰场的营业时间。目前,它显示为:
<tbody>
<tr>
<th>Days</th>
<th>Opening Time</th>
<th>Closing Time</th>
</tr>
<?php
$days_arr = ['','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
unset($days_arr[0]);
foreach($rink->businessHours as $hk => $hv){
for($i=1;$i<8;$i++){
if($hv->days == $i){
?>
<tr>
<td>{{$days_arr[$i]}}</td>
<td>{{$hv->opening_time}}</td>
<td>{{$hv->closing_time}}</td>
</tr>
<?php
}
}
}
?>
</tbody>
目前显示为:
Thu
但是,在这里,您可以看到and没有任何行Sun
。现在,我想用如下所示的方式显示该行:
Thu | - | - |
Sun | - | - |
目前,$rink->businessHours
包含以下数据:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\BusinessHours Object
(
[attributes:protected] => Array
(
[id] => 19
[days] => 1
[opening_time] => 07:00AM
[closing_time] => 08:00PM
[timezone] => MST
[rink_id] => 4
[created_at] => 2019-07-25 06:00:54
[updated_at] => 2019-07-25 06:00:54
)
)
[1] => App\Models\BusinessHours Object
(
[attributes:protected] => Array
(
[id] => 20
[days] => 2
[opening_time] => 07:00AM
[closing_time] => 08:00PM
[timezone] => MST
[rink_id] => 4
[created_at] => 2019-07-25 06:00:54
[updated_at] => 2019-07-25 06:00:54
)
)
[2] => App\Models\BusinessHours Object
(
[attributes:protected] => Array
(
[id] => 21
[days] => 3
[opening_time] => 07:00AM
[closing_time] => 08:00PM
[timezone] => MST
[rink_id] => 4
[created_at] => 2019-07-25 06:00:54
[updated_at] => 2019-07-25 06:00:54
)
)
[3] => App\Models\BusinessHours Object
(
[attributes:protected] => Array
(
[id] => 23
[days] => 5
[opening_time] => 07:00AM
[closing_time] => 08:00PM
[timezone] => MST
[rink_id] => 4
[created_at] => 2019-07-25 06:00:54
[updated_at] => 2019-07-25 06:00:54
)
)
[4] => App\Models\BusinessHours Object
(
[attributes:protected] => Array
(
[id] => 24
[days] => 6
[opening_time] => 07:00AM
[closing_time] => 01:00PM
[timezone] => MST
[rink_id] => 4
[created_at] => 2019-07-25 06:00:54
[updated_at] => 2019-07-25 06:00:54
)
)
)
解决方案
这将首先创建一个包含营业时间的数组,然后显示该数组。首先创建小时,将所有日期和打开和关闭时间作为“-”,然后对于每个实际打开时间,这些会覆盖虚拟值。这意味着任何缺失的日期都将显示这些虚拟值......
$days_arr = [ 1=> 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
$hours = array_fill(1, 7, ['opening_time' => '-', 'closing_time' => '-']);
foreach ( $rink->businessHours as $hk => $hv){
$hours[$hv->days] = ['opening_time' => $hv->opening_time,
'closing_time' => $hv->closing_time];
}
然后显示...
foreach($hours as $hk => $hv){
?>
<tr>
<td>{{$days_arr[$hk]}}</td>
<td>{{$hv['opening_time']}}</td>
<td>{{$hv['closing_time']</td>
</tr>
<?php
}
请注意,对于 day 数组,您可以说从 1 开始键,而不必添加一个空值然后删除它。此外,您for
不需要最后一个循环,因为您可以使用直接访问 days 数组$days_arr[$hk]
。
推荐阅读
- stable-baselines - 为什么稳定基线评估助手需要环境?
- pdf-generation - 尽管处于等待功能中,Puppeteer 没有及时返回?
- .net - 如何通过 Hello swagger 控制多个项目。例如我想选择一个双 UI 选择
- php - VueJs 作为单独安装或在 Laravel 中使用 VueJs
- python - 在 open3d-python 中更新相机内在参数
- python-sphinx - 狮身人面像页脚更新
- c# - 应该抛出异常还是该方法应该简单地返回?
- c# - Excel在公式中添加“@”符号
- sqlite - Xamarin.Forms SQLiteExtensions 多对多关系未检索
- r - 在给定条件下,在大型数据框中找到正确的单元格值