突出显示当前日期从db获取的表行

时间:2015-01-28 01:08:59

标签: php mysql html-table

使用下面的代码我从数据库中获取一个表。第一列有一个日历,我想知道是否有可能突出显示包含当天的表格的行。

我也用这个脚本显示当天:

    <?php
        date_default_timezone_set("Europe/Rome");
         echo " Italy: " . date("h:i:sa");
         echo " day " . date("d/m/Y") . "<br>";
          ?>

---表格代码---

     <?php
     $query = "SELECT * FROM trip"; 
                $result = mysql_query($query);

                echo "<table >"; // start a table tag in the HTML

                while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
                echo "<tr>
                            <td>" . $row['Day'] . "</td>
                            <td>" . $row['Country'] . "</td>
                            <td>" . $row['Place'] . "</td>
                            <td>" . $row['Flight'] . "</td>
                        </tr>";  //$row['index'] the index here is a field name
                }

                echo "</table>"; //Close the table in HTML
                mysql_close(); //Make sure to close out the database connection

            ?>

--- css CODE -

table
{
font-family: verdana;

color: white;
background: #003366;
text-align:center;
font-size:16px;
border-collapse: collapse; 

}
tr   {

padding-top:5px;
padding-bottom:5px;
font-size:16px;

}

tr:hover{

color:#003366;
background: white;

}

---添加课程。今天

.today { font-family: verdana; background: red; color: #003366; }

3 个答案:

答案 0 :(得分:1)

只是改变:

echo "<tr>
    <td>" . $row['Day'] . "</td>
    <td>" . $row['Country'] . "</td>
    <td>" . $row['Place'] . "</td>
    <td>" . $row['Flight'] . "</td>
</tr>";  //$row['index'] the index here is a field name
}

使用:

if ($row['Day'] ==  date("Y-m-d") ) {
    echo '<tr class="today">';
} else {
    echo "<tr>";
}
echo "<td>" . $row['Day'] . "</td>
    <td>" . $row['Country'] . "</td>
    <td>" . $row['Place'] . "</td>
    <td>" . $row['Flight'] . "</td>
</tr>";  //$row['index'] the index here is a field name
}

并将类today添加到您的css文件中。

对不起,我无法调试这个,但我认为你有一个想法

答案 1 :(得分:0)

由于您使用的是mysql API,我将假设这是一个现有的遗留应用程序。如果不是,我强烈建议您使用mysqli / PDO API和预处理语句。你做得越早越好,你将来会更加满意你决定放弃mysql 关于您的问题,您可以通过格式化现有日期/时间戳并将其与PHP DateTime类进行比较来完成此操作。

<强> PHP:

$today = new DateTime('today'); // create new DT object representing today

while($row = mysql_fetch_array($result)){
    $dbDate = new DateTime($row['Day']); // converts database date to DT object
    $dbDate = $dbDate->format('Y-m-d'); // format as only date, remove time
    echo ($dbDate == $today) ? "<tr><td class=\"today\">" . $row['Day'] . "</td>" :
    "<tr><td>" . $row['Day'] . "</td>";
    echo "<td>" . $row['Country'] . "</td>
    <td>" . $row['Place'] . "</td>
    <td>" . $row['Flight'] . "</td>
    </tr>";
}

CSS:

.today {
  //the style you want//
}

答案 2 :(得分:0)

在表格中使用循环,查找单元格是否有当前日期。如果它然后使用jquery突出显示单元格。你可以这样做:

 var d = new date();
 table.find('tr td').each(function(d){
   if($(this).text() == d) {
     $(this).css("background", "red");
   }
 });