I’m showing MySQL table results on my page and I’m trying to highlight the rows where the Next review date is earlier than today’s date, these rows should be colored red (later I would want another coloring as well, today+10 days should be in orange). I know that I would need to use Javascript for that, however, I have tried 10+ solutions but none of them are working, could you help me? The Next review date is in date format in phpmyadmin (YYYY-MM-DD).
Here is what I have tried currently (the id of my table is table):
var table = document.getElementById("table");
for(var i = 0; i < table.rows.length; i ++) {
var status = new Date(table.rows[i].cells[4].innerHTML).getTime();
var now = new Date().getTime();
console.log(now>status)
if (now>status) {
table.rows[i].style.background = "red";
}
};
If you would need more code or explanation please indicate it and I will edit the post. Thank you very much for your help!
This is how the table looks like now (coloring was never successful):
if ($_SESSION['user_type'] == 'admin') {
echo '<div class="table" id="table"> <table>
<tr>
<th>
<a href="?orderBy=name">Asset name</a>
</th>
<th>
<a href="?orderBy=classification">Classification</a>
</th>
<th>
<a href="?orderBy=tag">Tag</a>
</th>
<th>
<a href="?orderBy=department">Department</a>
</th>
<th>
<a href="?orderBy=nextreview">Next review date</a>
</th>
<th>
<a href="?orderBy=responsible">Responsible</a>
</th>
<th>Update</th>
</tr>';
$sql = "SELECT *
FROM `assets`
ORDER BY id";
if (isset($_GET['orderBy'])) {
$orderby = $_GET['orderBy'];
if ($orderby == 'name')
{
$sql = "SELECT * FROM `assets`ORDER BY Asset_name";
}
elseif ($orderby == 'classification')
{
$sql = "SELECT * FROM `assets`ORDER BY Classification";
}
elseif ($orderby == 'tag')
{
$sql = "SELECT * FROM `assets`ORDER BY Tag";
}
elseif($orderby == 'department')
{
$sql = "SELECT * FROM `assets`ORDER BY Department";
}
elseif($orderby == 'nextreview')
{
$sql = "SELECT * FROM `assets` ORDER BY Review_date asc";
}
elseif($orderby == 'responsible')
{
$sql = "SELECT * FROM `assets`ORDER BY Responsible";
}
}
if (!($result=mysqli_query($conn, $sql)))
{ die("Could not show the required data" . mysqli_error($conn));}
elseif (mysqli_num_rows($result) > 0) {
while($result2=mysqli_fetch_array($result))
{echo '<tr>
<td>'.$result2["Asset_name"].'</td>
<td>'.$result2["Classification"].'</td>
<td>'.$result2["Tag"].'</td>
<td>'.$result2["Department"].'</td>
<td class="status">'.$result2["Review_date"].'</td>
<td>'.$result2["Responsible"].'</td>
<td><a href="editassets.php?id='.$result2['id'].'">Edit</a> | <a href="deleteassets.php?id='.$result2['id'].'" onclick="return confirm('Do you want to delete this asset?');">Delete</a></td>
</tr>';
}
echo '</table></div>';
}
Current look:
2
Answers
ES<5 version
Sortable
Sorting HTML table with JavaScript
Note the numeric cells will be sorted alphabetically here, more code needed to handle the
90
belowAs others start answering, I will post what I discussed with you in comments:
Your HTML has:
Your JavaScript however defines:
… and continues to treat that
table
as the table element, but it isn’t. In your HTML you can see it is a<div>
element, not a<table>
element.So align both. Either change your HTML, so that the
<table>
has theid="table"
:… or change your JavaScript so that it takes the child element of the
<div>
with that id:NB: Don’t make both changes: choose the one that best suits you.