使用数据库中的值突出显示HTML表格单元格

时间:2019-07-16 08:05:03

标签: php

我想通过将给定日期的时间值与数据库表进行比较来突出显示HTML表中的时间值。

我正在尝试这段代码

<?php

     function getdata($start_date, $timevalue)
     {
        $host = "localhost";
        $user = "root";
        $password = "";
        $database = "test";

        $con = mysqli_connect($host,$user,$password,$database);
        $result = $con->query( "SELECT * FROM booking" );
        while($row = $result->fetch_assoc()) {
                if(($row['datefrom']==$start_date) && ($row['timefrom']<=$timevalue && $row['timeto']>=$timevalue)){
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
     }

    $start_date = "2019-07-11";
    $end_date = "2019-07-15";
    $tmfrm = strtotime("07:00:00");
    $tmto = strtotime("15:00:00");

    echo "<table border='1'>";
     while (strtotime($start_date) <= strtotime($end_date))
     {
        echo "<tr><td>".$start_date."</td>";
        for($t=$tmfrm; $t<=$tmto; $t+=1800)
        {
            $timevalue=date("H:i:s", $t);
            if (getdata($start_date, $timevalue)==1){
            echo "<td bgcolor='red'>". $timevalue  . "<br></td>";
            }else
            {
                echo "<td bgcolor='green'>".  $timevalue  . "<br></td>";
            }
        }
        echo "</tr>";
        $start_date = date ("Y-m-d", strtotime("+1 days", strtotime($start_date)));
      }
      echo "</table>"; 
?>

下面是带有示例数据的表

booking
--------------------------------------------
roomno | datefrom   |  timefrom | timeto   | 
--------------------------------------------
room1  | 2019-07-11 |  07:00:00 | 10:00:00 |
--------------------------------------------
room1  | 2019-07-11 |  11:00:00 | 12:00:00 |
--------------------------------------------
room1  | 2019-07-12 |  11:00:00 | 12:00:00 |
--------------------------------------------
room1  | 2019-07-13 |  11:00:00 | 12:00:00 |
--------------------------------------------
room1  | 2019-07-14 |  07:00:00 | 10:00:00 |
--------------------------------------------
room1  | 2019-07-15 |  10:30:00 | 13:30:00 |
--------------------------------------------
room1  | 2019-07-11 |  13:30:00 | 14:00:00 |
--------------------------------------------

在我得到的输出中,它仅突出显示了2019-07-11的07:00至10:00的时间,而应突出显示其他日期的时间,包括其他时间(14:00到15: 00)表示2019-07-11。 (见图)。逻辑有什么问题?

view output here

1 个答案:

答案 0 :(得分:0)

我已经找到解决方案。我要做的就是修改getdata()函数使其不循环。我用select语句指定条件,而不是在if语句循环中检查条件。以下是修改后的代码:

function getdata($start_date, $timevalue)
     {
        $host = "localhost";
        $user = "root";
        $password = "";
        $database = "test";

        $con = mysqli_connect($host,$user,$password,$database);
        $result = mysqli_query($con, "SELECT * FROM booking WHERE datefrom='$start_date' AND (timefrom<='$timevalue' AND timeto>='$timevalue')" );

         if(mysqli_num_rows( $result)>0){
                    return 1;
        }
         else{
             return 0;
         }
     }

现在运行正常。以下是我得到的输出。这就是我所需要的。 Output