Robert Francis
asked on
Change background color in td if any value is 1
I would like to change the background color of the td if any of the values equals 1. I want to try to avoid creating if statements for every line.
Here is the code:
Here is the code:
<?php while ($row = sqlsrv_fetch_array($results)){
$id = $row['id'];
$ename= $row['ename'];
$first_name= $row['first_name'];
$last_name= $row['last_name'];
$password= $row['password'];
$jobnotes = $row['jobnotes'];
$sales_rep = $row['sales_rep'];
$time_fix = $row['time_fix'];
$time_fix_m = $row['time_fix_m'];
$supervisor = $row['supervisor'];
$wc_supervisor = $row['wc_supervisor'];
$quoter = $row['quoter'];
$admin_all = $row['admin_all'];
$material_cost = $row['material_cost'];
$material_edit = $row['material_edit'];
$assets = $row['assets'];
$active = $row['active'];
$nc_create = $row['nc_create'];
$nc_respond = $row['nc_respond'];
$nc_admin = $row['nc_admin'];
$comment_short_delivery = $row['comment_short_delivery'];
$short_del = $row['short_del'];
$nc_reports = $row['nc_reports'];
$costing = $row['costing'];
$schedule = $row['schedule'];
$enercon_interest = $row['enercon_interest'];
$routings_edit = $row['routings_edit'];
$front_office = $row['front_office'];
$admin_invoice = $row['admin_invoice'];
$admin_purch = $row['admin_purch'];
$add_meeting_minutes = $row['add_meeting_minutes'];
$ca_create = $row['ca_create'];
$ca_action = $row['ca_action'];
$ca_audit = $row['ca_audit'];
$admin_portal = $row['admin_portal'];
$iso_reports = $row['iso_reports'];
$iso_respond = $row['iso_respond'];
$pmcs = $row['pmcs'];
$promised_date = $row['promised_date'];
$admin_maint = $row['admin_maint'];
?>
<tr>
<td><?php echo $ename;?></td>
<td><?php echo $first_name;?></td>
<td><?php echo $last_name;?></td>
<td><?php echo $password;?></td>
<td><?php echo $jobnotes;?></td>
<td><?php echo $sales_rep;?></td>
<td><?php echo $time_fix;?></td>
<td><?php echo $time_fix_m;?></td>
<td><?php echo $supervisor;?></td>
<td><?php echo $wc_supervisor;?></td>
<td><?php echo $quoter ;?></td>
<td><?php echo $admin_all ;?></td>
<td><?php echo $material_cost ;?></td>
<td><?php echo $material_edit ;?></td>
<td><?php echo $assets;?></td>
<td><?php echo $active;?></td>
<td><?php echo $nc_create ;?></td>
<td><?php echo $nc_respond;?></td>
<td><?php echo $nc_admin;?></td>
<td><?php echo $comment_short_delivery;?></td>
<td><?php echo $short_del;?></td>
<td><?php echo $nc_reports;?></td>
<td><?php echo $costing;?></td>
<td><?php echo $schedule;?></td>
<td><?php echo $enercon_interest;?></td>
<td><?php echo $routings_edit;?></td>
<td><?php echo $front_office;?></td>
<td><?php echo $admin_invoice;?></td>
<td><?php echo $admin_purch;?></td>
<td><?php echo $add_meeting_minutes;?></td>
<td><?php echo $ca_create;?></td>
<td><?php echo $ca_action;?></td>
<td><?php echo $ca_audit;?></td>
<td><?php echo $admin_portal;?></td>
<td><?php echo $iso_reports;?></td>
<td><?php echo $iso_respond;?></td>
<td><?php echo $pmcs;?></td>
<td><?php echo $promised_date;?></td>
<td><?php echo $admin_maint;?></td>
<td><a href="portal_admin_edit.php?id=<?php echo $row['id'];?>">Edit</a></td>
<td><a href="portal_admin_delete.php?id=<?php echo $row['id'];?>">Delete</a></td>
</tr>
<?php } ?>
ASKER
But won't this change every TD if 1 appears anywhere in the array?
Oh Sorry!, misunderstood your question. In this case you will have to use if statement on each td anyway.
Do you have CSS divs or classes associated with these TD elements? CSS might make this a lot easier.
ASKER
I was going to do something like this:
and then this:
But I would have to do that for every column. Was trying to avoid that.
if ($jobnotes == 1){$jobnotescss = 'bg-yellowgreen';} else {$jobnotescss = '';}
and then this:
<td class='<?php echo $jobnotescss;?>'><?php echo $jobnotes;?></td>
But I would have to do that for every column. Was trying to avoid that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I will look into all of that. Once again you are awesome.
Thanks for your kind words, and thanks for using E-E!
One thing I noticed above was a link to portal_admin_delete.php. If that allows someone to delete a row from the database via a GET request, be very careful that it never gets searched by a spider like Google. As long as it is behind a password protection and requires an "Are you sure?" confirmation, you should be OK. If you're not clear on the behavior of this stuff, it might be worth posting a question about it, just to be safe.
Best regards, ~Ray
One thing I noticed above was a link to portal_admin_delete.php. If that allows someone to delete a row from the database via a GET request, be very careful that it never gets searched by a spider like Google. As long as it is behind a password protection and requires an "Are you sure?" confirmation, you should be OK. If you're not clear on the behavior of this stuff, it might be worth posting a question about it, just to be safe.
Best regards, ~Ray
You can do this with jQuery as well
Working sample here
EDIT:
Removed superfluous code
<script>
$(function() {
// ASSUMES TABLE ID IS mytable
$('#mytable td').each(function(i,e) {
if (e.textContent == 1) {
e.style.backgroundColor = 'red';
}
});
});
</script>
Working sample here
EDIT:
Removed superfluous code
You can use in_array,
if (in_array("1", $row)) {
//Code to change td color here...
}
this will check if 1 is available in $row array