0

I would like to have a dynamic style color base on the database

// the first line is static
$row_color = $value['late'] >= 60 ? ' style="color:red;"' : '';$

// and this code is not working
$color = $value['color'];
$row_color = $value['status'] == "OT" ? ' style="color: $color"' : '';
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
NOOB
  • 33
  • 6

2 Answers2

3

You cannot put php in css file. What you can do is you can create two different classes in your .css file, and in php page you can load those classes depending on the condition.

e.g. file.css may have

.color-red { color: red; }
.color-blue { color: blue; }

in file.php you can put

$color = ($value['late'] >= 60) ? 'color-red' : 'color-blue';

in your html tag you can use it like

<div class="<?php echo $color?>">
1

you just need to concate $color here

$color = $value['color'];
$row_color = $value['status'] == "OT" ? 'style="color:'.$color.';"' : '';

if you are passing this to view then just do

$color = $value['color'];
$data['row_color'] = $value['status'] == "OT" ? 'style="color:'.$color.';"' : '';

And pass this $data to view and use it as variable line i.e

<div <?php echo $row_color; ?> ></div>
<span <?php echo $row_color; ?> ></span>
M.Hemant
  • 2,345
  • 1
  • 9
  • 14