Re: change td bgcolor based on mysql value
Posted: Sat Jan 16, 2016 8:13 am
You mean something like this? http://codepen.io/anon/pen/yezvqg
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
// connect to the database
include('connect-db.php');
// get the records from the database
$result = $mysqli->query("SELECT id, software_title, software_number, times_used, customers_name FROM software ORDER BY id");
?>
<style>
.red {
background-color: #ff0000;
}
.grey {
background-color: #d4d4d4;
}
</style>
<?php if ($result && $result->num_rows > 0): ?>
<table class="records">
<tr>
<th>ID</th>
<th>Software Title</th>
<th>Product_key</th>
<th>Times Used</th>
<th>Customers Name</th>
<th colspan="1">Actions</th>
</tr>
<?php while ($row = $result->fetch_object()): ?>
<?php $class = $row->times_used == 3 ? 'red' : 'grey'; ?>
<tr>
<td><?= $row->id; ?></td>
<td><?= $row->software_title; ?></td>
<td><?= $row->software_number; ?></td>
<td class="<?= $class; ?>"><?= $row->times_used; ?></td>
<td><?= $row->customers_name; ?></td>
<td><a href="add-edit-software-used.php?id=<?= $row->id; ?>">Edit</a></td>
</tr>
<?php endwhile; ?>
</table>
<?php else: ?>
<p>No results to display!</p>
<?php endif; ?>Code: Select all
<?php
function switchColor($rowValue) {
//Define the colors first
$color1 = '#e2e2e2';
$color2 = '#00ff00';
$selected = $color2;
/*Change the 'cases' to whatever you want them to be,
so if you want to change the color according to
occupation, write down the possible occupations or if
the color changes according to gender, name the gender
names that come out of the database (eg. case 'male':).*/
switch ($rowValue) {
case '3':
$selected = $color1;
break;
default:
$selected = $color2;
}
return $selected;
}Code: Select all
if ($result = $mysqli->query("SELECT id, software_title, software_number, times_used, customers_name FROM software ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
if ($result && $result->num_rows > 0);Code: Select all
while ($row = $result->fetch_object())
$class = $row->times_used == 3 ? 'red' : 'grey';
{Code: Select all
echo "<td class='<?= $class; ?>'>"Code: Select all
echo "<td class='$class'>";