Home > OS >  Conditional formatting php table
Conditional formatting php table

Time:06-15

I need help with a simple task, but I am stuck... I have a database where I pull different data and I am visualizing those data on a web page. Here is my query for getting the data:

$result = mysqli_query($mysqli, "SELECT *, DATEDIFF(nextcalibration, CURDATE()) AS days FROM tools AS dp ");

Here is my table:

<table id="table_id" >

<tr>
    <th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php

while($user_data = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".$user_data['toolnr']."</td>";
    echo "<td>".$user_data['status']."</td>";
    echo "<td>".$user_data['toolname']."</td>";
    echo "<td>".$user_data['serial']."</td>";
    echo "<td>".$user_data['usedat']."</td>";
    echo "<td>".$user_data['owner']."</td>";
    echo "<td>".$user_data['calibrated']."</td>";
    echo "<td>".$user_data['nextcalibration']."</td>";
    echo "<td>".$user_data['vendors']."</td>";
    echo "<td>".$user_data['days']. "</td>";
    echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}

?>
</table>

I would like to make conditional formatting only for the last column - "days". The idea is if the value of "days" <= 30 to become red color text. I tried with various JS, but honestly, it did not work.

CodePudding user response:

<table id="table_id" >

<tr>
    <th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php

while($user_data = mysqli_fetch_array($result)) 
{
    echo "<tr>";
    echo "<td>".$user_data['toolnr']."</td>";
    echo "<td>".$user_data['status']."</td>";
    echo "<td>".$user_data['toolname']."</td>";
    echo "<td>".$user_data['serial']."</td>";
    echo "<td>".$user_data['usedat']."</td>";
    echo "<td>".$user_data['owner']."</td>";
    echo "<td>".$user_data['calibrated']."</td>";
    echo "<td>".$user_data['nextcalibration']."</td>";
    echo "<td>".$user_data['vendors']."</td>";
    echo "<td>";
    $days=$user_data['days'];
    if($days <= 30){
        echo "<span style='color:red;'>$days</span>";
    }else{
        echo $days;
    }
    echo "</td>";
    echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}

?>
</table>

CodePudding user response:

Would something simple like this - without JS- work?

<table id="table_id" >

<tr>
    <th>Nr</th> <th>Status</th> <th>name</th> <th>Serial</th> <th>Used At</th> <th>Owner</th> <th>Calibrated</th> <th>nextcalibration</th> <th>vendor</th> <th>days</th> <th>actions</th>
</tr>
<?php

while($user_data = mysqli_fetch_array($result)) 
{
    $style_days=($user_data['days']<=30)?'style="color:red"':'';
    echo "<tr>";
    echo "<td>".$user_data['toolnr']."</td>";
    echo "<td>".$user_data['status']."</td>";
    echo "<td>".$user_data['toolname']."</td>";
    echo "<td>".$user_data['serial']."</td>";
    echo "<td>".$user_data['usedat']."</td>";
    echo "<td>".$user_data['owner']."</td>";
    echo "<td>".$user_data['calibrated']."</td>";
    echo "<td>".$user_data['nextcalibration']."</td>";
    echo "<td>".$user_data['vendors']."</td>";
    echo "<td ".$style_days.">".$user_data['days']. "</td>";
    echo "<td><a href='edit.php?id=$user_data[id]'><img src='img/edit.png' ></a> | <a href='delete.php?id=$user_data[id]' onclick='return checkDelete()'><img src='img/delete.png'></a></td></tr>";
}

?>
</table>

Of course, apply the necessary guarding for $user_data['days'] (existence, numeric..) :)

  • Related