douyong1850 2016-01-21 22:42
浏览 33
已采纳

根据数据库的输入,使用PHP突出显示表中的单元格[关闭]

I am trying to display a timetable of sorts on my webpage. I need to use PHP to highlight table cells depending on whether they have an entry to the database for todays date. I currently have a table with rooms across the top and times down the side as below:

echo "<table border-1>
    <tr>
        <th></th>
        <th>B-12</th>
        <th>G.05</th>
        <th>G.06</th>
        <th>1.04</th>
        <th>1.05</th>
        <th>1.06</th>
        <th>2.04</th>
        <th>2.05</th>
        <th>2.06</th>
        <th>3.04</th>
        <th>3.05</th>
        <th>3.06</th>
    </tr>

    <tr>
        <th>9.00</th>
        <td></td>
    </tr>
    <tr>
        <th>9.30</th>
        <td></td>
    </tr>
    <tr>
        <th>10.00</th>
        <td></td>
    </tr>
    <tr>
        <th>10.30</th>
        <td></td>
    </tr>";

I need to read entries to my database and highlight the corresponding cell, in the correct room column and in the correct time row, based on the entry date, like this

Is there a way to do this in PHP? Am I going about this all wrong? I am new to PHP so am not entirely sure what is the best method to achieve what I need.

I have searched everywhere for a solution to this but have found none that answer my question. If anyone has any idea or could advise me on the best method I would really appreciate it.

  • 写回答

1条回答 默认 最新

  • duanniu3385 2016-01-22 00:39
    关注

    I am still a bit confused if you question is more about how to interact with mysql or how to display occupancy/availability in each room.

    I assume it is the latter. I also assume you know how to get your data from mysql and you just need a way on how to display the occupancy.

    <?php
    
    $roomsAvailability = [
        '1' => ['9:30', '10:30'],
        '2' => ['10:00'],
        '3' => ['9:00', '11:00'],
    ];
    
    $times = ['8:30', '9:00','9:30', '10:00', '10:30',  '11:00'];
    
    ?>
    <style>
    table, th, td {
        margin:5px;
        border: 1px solid black;
    }
    .available {
        background-color: lightblue;
    }
    </style>
    <table>
        <tr><th>&nbsp;</th><th>g1</th><th>g2</th><th>g3</th></tr>
    <?php
    
    foreach($times as $time)
    {?>
        <tr>
        <?php
            echo "<td>$time</td>";
            foreach($roomsAvailability as $roomNumber => $roomAvailability)
            {
                if(in_array($time , $roomAvailability))
                {
                    echo "<td class='available'>&nbsp;</td>";
                }
                else{
                    echo "<td class='notAvailable'>&nbsp;</td>";
                }
            }
        ?>
        </tr>
    <?php
    }
    
    ?>
    </table>
    

    In the script above you can see how i represent the availability in a variable for the rooms (lets assume you get this array from the database). Also the times (for instance times of business/slots) are also in the db.

    In general in a db you would have relations and more elaborate things that I have skipped from the answer.

    The script displays a table similar to the one you added in your question with cells where the room is allocated marked with a lightblue background.

    You can copy and run it on PHP Fiddle to see the output.

    Please let me know if it is what you were looking for

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料