使用下面的代码,我正在从数据库中获取一个表。第一列有一个日历,我想知道是否可能突出显示包含当前日期的表格行。
我还使用以下脚本显示当前一天:
<?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 代码--
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;
}
只需更改:
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 文件中。
抱歉,我无法调试这个,但我认为你有一个想法
使用循环访问表并查找单元格是否有当前日期。如果有,则使用 jquery 突出显示单元格。你可以这样做:
var d = new date();
table.find('tr td').each(function(d){
if($(this).text() == d) {
$(this).css("background", "red");
}
});
由于您使用的是 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//
}