Reputation: 59
If time2 is bigger than time 1 I want the background color change to red.
I'm a totally noob in this and I tried now for days but not getting it to work.
In CSS I have .bgred {background: red;}
<?php
$sql = ("SELECT name, time1, time2 FROM myTable WHERE dates LIKE '".$_POST['dat']."' ");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table action='' method='POST'><tr>
<th>Name</th>
<th>Time1</th>
<th>Time2</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$var1 = $row["time1"];
$var2 = $row["time2"];
if($var2 < $var1) {
$bgred = 'class="bgred"';
} else {
$bgred = '';
}
echo "<tr><td>".$row["name"]."</td>
<td>".$row["time1"]."</td>
<td class="$bgred">".$row["time2"]."</td></tr>";
}
}
echo "</table>";
mysqli_close($conn);
?>
Upvotes: 0
Views: 1858
Reputation: 486
You are not echoing out $bgred anywhere.
<?php
$sql = ("SELECT name, time1, time2 FROM myTable WHERE dates LIKE '".$_POST['dat']."' ");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table action='' method='POST'><tr>
<th>Name</th>
<th>Time1</th>
<th>Time2</th></tr>";
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$var1 = $row["time1"];
$var2 = $row["time2"];
//edited here
if(strtotime($var2) < strtotime($var1)) {
$bgred = 'bgred';
//done with edit
} else {
$bgred = '';
}
echo "<tr><td class='$bgred'>".$row["name"]."</td>
<td class='$bgred'>".$row["time1"]."</td>
<td class='$bgred'>".$row["time2"]."</td></tr>";
}
}
echo "</table>";
mysqli_close($conn); ?>
Upvotes: 1
Reputation: 1458
$bgred = null;
$rows = null;
while($row = mysqli_fetch_assoc($result)) {
$var1 = $row["time1"];
$var2 = $row["time2"];
if($var2 < $var1) {
$bgred = 'class="bgred"';
} else {
$bgred = '';
}
$rows .= '<tr>
<td>' . $row["name"] . '</td>
<td>' . $row["time1"] . '</td>
<td>' . $row["time2"] . '</td>
</tr>';
}
echo '<table action="" method="POST">
<tr>
<th>Name</th>
<th>Time1</th>
<th class="' . $bgred . '">Time2</th>
</tr>
' . $rows . '
</table>';
Do you have a reason to use double quotes?
Upvotes: 0