Please give me some advice, i have a few ideas but not working so well …
I have a database with defect type, urgency, description etc.
i would like to show all defects on the page but if it is urgent it must make the row red, if not urgent green and so on. getting the data and displaying it i got right just struggling with the color coding
Use if statements for this. Just save the rows as their urgent level. Then based on that level, use if statements and then apply the CSS according to the urgent level.
while ($row ...
$class_value = '';
if ($row['urgency'] === "urgent") {
$class_value = "urgent";
.....
<tr class="<?php echo $class_value; ?>">
// depending on how you're putting together the output
displaydefect.php (2.7 KB)
I think i`m almost there just very strange if someone can help me, sure it is something stupid.
It actually colors the row RED that is Urgent but only every second one … and it does not color my second if green. so basically only Urgent is showing and Not Urgent is not showing.
I don’t know why you have the second if() clause - can the urgency be anything other than “urgent” or “not urgent”? If it cannot, the second if() is redundant. ETA - oh, and typo:
if($row=["urgency"]==="Not Urgent"){
As you use CSS to style the output, you probably need to show the CSS for anyone to help much.
Do you mean that it does not colour the “not urgent” rows, or that they do not show at all? Typo above would probably cause the latter.
I would use a switch() construction for that. I would also change the database so that instead of using text for the status, I would use a code 1, 2, 3 or 4.
One easy way to simplify the code here would be to create an associative array where the index is the possible values for the status, and the data for each is your class name.
$classname = array("urgent" => "urgent", "not urgent" => "nurgent", ... and so on
then you can do away with all the ifs and cases and just use something like
// at the start of each iteration of the while() loop
if (exist($classname[$row['urgency']])) {
$cls = $classname[$row['urgency']];
}
else {
$cls = "unknown"; // or however you want to style invalid codes
}
// and then for each column do something like:
echo "<td class='" . $cls . "'>{$row["type"]} </td>";
You could do a “view source” on your page and see if that gives any clues. Post the html here if it doesn’t. Is “class=data-table” adding anything like an nth-child rule?
So urgent rows will get class urgency-urgent, non urgent will get urgency-not-urgent and you can then just style those. No if, no switch, just straight forward mapping to CSS classes.
Added bonus is if you ever want a color for another urgency or even if you add a completely new urgency and you want a color for that you can just add the CSS code and you’re done, no changes needed to the PHP code at all.