首页 > 解决方案 > 如果在表中找不到行,则使用连字符 (-) 显示数据 - 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
                        )


                )

)

标签: phplaravel

解决方案


这将首先创建一个包含营业时间的数组,然后显示该数组。首先创建小时,将所有日期和打开和关闭时间作为“-”,然后对于每个实际打开时间,这些会覆盖虚拟值。这意味着任何缺失的日期都将显示这些虚拟值......

$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]


推荐阅读