突出显示当天从数据库中获取的 table 行
Highlight table row fetched from db with current day
使用下面的代码,我从我的数据库中获取 table。第一列有一个日历,我想知道是否可以突出显示包含当前日期的 table 行。
我也用这个脚本显示当天:
<?php
date_default_timezone_set("Europe/Rome");
echo " Italy: " . date("h:i:sa");
echo " day " . date("d/m/Y") . "<br>";
?>
--- TABLE 代码 ---
<?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;
}
--- 添加 CLASS .TODAY
.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
}
并将 class today
添加到您的 css 文件。
抱歉,我无法对此进行调试,但我认为你有一个想法
由于您使用的是 mysql API,我假设这是一个现有的遗留应用程序。如果不是,我强烈建议您使用 mysqli/PDO API 和准备好的语句。你越早这样做越好,你会在未来对你离开 mysql 的决定感到高兴。
关于您的问题,您可以通过格式化现有的 date/timestamp 并将其与 PHP DateTime
class.
进行比较来实现
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//
}
使用循环遍历 table 并查找单元格是否包含当前日期。如果有,则使用 jquery 突出显示该单元格。你可以这样做:
var d = new date();
table.find('tr td').each(function(d){
if($(this).text() == d) {
$(this).css("background", "red");
}
});
使用下面的代码,我从我的数据库中获取 table。第一列有一个日历,我想知道是否可以突出显示包含当前日期的 table 行。
我也用这个脚本显示当天:
<?php
date_default_timezone_set("Europe/Rome");
echo " Italy: " . date("h:i:sa");
echo " day " . date("d/m/Y") . "<br>";
?>
--- TABLE 代码 ---
<?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;
}
--- 添加 CLASS .TODAY
.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
}
并将 class today
添加到您的 css 文件。
抱歉,我无法对此进行调试,但我认为你有一个想法
由于您使用的是 mysql API,我假设这是一个现有的遗留应用程序。如果不是,我强烈建议您使用 mysqli/PDO API 和准备好的语句。你越早这样做越好,你会在未来对你离开 mysql 的决定感到高兴。
关于您的问题,您可以通过格式化现有的 date/timestamp 并将其与 PHP DateTime
class.
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//
}
使用循环遍历 table 并查找单元格是否包含当前日期。如果有,则使用 jquery 突出显示该单元格。你可以这样做:
var d = new date();
table.find('tr td').each(function(d){
if($(this).text() == d) {
$(this).css("background", "red");
}
});