php - Laravel 创建动态时间表

标签 php html mysql laravel

我有一个医院表和日程表,我想使用这些表创建一个日程表 html

+----+--------------+
| id |     name     |
+----+--------------+
|  1 |  hospital a  |
|  2 |  hospital b  |
+----+--------------+

+-----+---------+--------+---------------+
| day |  start  |   end  |  hospital_id  |
+-----+---------+--------+---------------+
| mon |  10:00  |  14:00 |       1       |
| tue |  15:00  |  20:00 |       1       |
| wed |  14:00  |  19:00 |       2       |
| fri |  10:00  |  15:00 |       2       |
+-----+---------+--------+---------------+

问题是计划时间与正确的 <th> 不一致
我需要什么:

+-------+------+--------+-------+-------+-------+------+
| mon   |  tue  |  wed  |  thu  |  fri  |  sat  |  sun |
+------------------------------------------------------+
|                     hospital a                       |
+-------+-------+-------+-------+-------+-------+------+
| 10:00 | 15:00 |       |       |       |       |      |
|   -   |   -   |       |       |       |       |      |
| 14:00 | 20:00 |       |       |       |       |      |
+-------+-------+-------+-------+-------+-------+------+
|                     hospital b                       |
+-------+-------+-------+-------+-------+-------+------+
|       |       | 14:00 |       | 10:00 |       |      |
|       |       |   -   |       |   -   |       |      |
|       |       | 19:00 |       | 15:00 |       |      |
+-------+-------+-------+-------+-------+-------+------+

但是变成了这样

+-------+-------+-------+-------+-------+-------+------+
| mon   |  tue  |  wed  |  thu  |  fri  |  sat  |  sun |
+------------------------------------------------------+
|                     hospital a                       |
+-------+------+--------+-------+-------+-------+------+
| 10:00 | 15:00 |       |       |       |       |      |
|   -   |   -   |       |       |       |       |      |
| 14:00 | 20:00 |       |       |       |       |      |
+-------+-------+-------+-------+-------+-------+------+
|                     hospital b                       |
+-------+-------+-------+-------+-------+-------+------+
| 14:00 | 10:00 |       |       |       |       |      |
|   -   |   -   |       |       |       |       |      |
| 19:00 | 15:00 |       |       |       |       |      |
+-------+-------+-------+-------+-------+-------+------+

这是我的 .blade.php 脚本

<table class="table">
    <thead>
       <tr>
           <td class="text-center">mon</td>
           <td class="text-center">tue</td>
           <td class="text-center">wed</td>
           <td class="text-center">thu</td>
           <td class="text-center">fri</td>
           <td class="text-center">sat</td>
           <td class="text-center">sun</td>
        </tr>
    </thead>
    <tbody>
    @foreach($hospital as $hos)
        <tr>
            <td class="text-center _b" colspan="7">{{$hos->name}}</td>
        </tr>
        <tr>
        @foreach($schedule->where('hospital_id', $hos->id) as $sch)
        @if($sch->day == 'mon')
            <td class="text-center">
                <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                <div class="jam active">s/d</div>
                <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
            </td>
            @endif
            @if($sch->day == 'tue')
            <td class="text-center">
                <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                <div class="jam">s/d</div>
                <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
            </td>
            @endif
            @if($sch->day == 'wed')
            <td class="text-center">
                <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                <div class="jam">s/d</div>
                <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
            </td>
            @endif
            @if($sch->day == 'thu')
                <td class="text-center">
                    <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                    <div class="jam">s/d</div>
                    <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
                </td>
                @endif
                @if($sch->day == 'fri')
                <td class="text-center">
                    <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                    <div class="jam">s/d</div>
                    <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
                </td>
                @endif
                @if($sch->day == 'sat')
                <td class="text-center">
                    <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                    <div class="jam active">s/d</div>
                    <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
                </td>
                @endif
                @if($sch->day == 'sun')
                <td class="text-center">
                    <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
                    <div class="jam">s/d</div>
                    <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
                </td>
                @endif
                @endforeach
            </tr>
            @endforeach
        </tbody>
    </table>
</table>

发生这种情况是因为 <td></td>if 时不会创建条件不满足。但当我尝试移动 if里面td table 看起来更可怕

最佳答案

您需要输出一个空的<td></td>如果那天没有时间的话。

<?php
    $days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];   
?>

@foreach($schedule->where('hospital_id', $hos->id) as $sch)
    @foreach($days as $day)
      @if($sch->day == $day)
         <td class="text-center">
            <div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
            <div class="jam active">s/d</div>
            <div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
         </td>
      @else
         <td></td>
      @endif
    @endforeach
@endforeach

关于php - Laravel 创建动态时间表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40148261/

相关文章:

MySQL SELECT *优化

php - 如何计算用户提交的数据出现在存储在数据库问题中的文章中的次数

javascript - 调整页面大小时 div 下降

php - 在 SQL SELECT 语句和 PDO 中使用大于或等于 (>=) 和小于或等于 (<=)

html - 使两个 div 向右浮动并重叠?

javascript - 以编程方式检查网络资源是否已加载到网页

mysql - Facebook 唯一标识符

mysql - SELECT * FROM 表 其中 CURDATE()

php - 如何启用 IMAP 模块

php - 将 Javascript 日期字符串转换为 PHP 日期