简体   繁体   中英

PHP colour coding based on colour

I am trying to learn PHP whilst creating a local dashboard for Coronavirus outbreaks I have been doing pretty well in my opinion but really stuck on this.

I am trying to pull data from my database and colour code it depending on the result.

I am able to display it fine in the format I want by using the following code but getting very confused with how to display it in colours

the numbers I am working with are from 0.00000000 which would be green to 0.00104348 which would be red.

Any help would be greatly appreciated

$sql = "SELECT FORMAT(conf / pop, 8) as pop_inf from cases ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)){
        echo $row['pop_inf'] . "%";
    }
}

If your intention here is to create a gradient scale of red for each value based on its range then the easiest solution is to map the range to the color code.

For example

const MAX = 0.00104348;
// this gives you the percentage of red (from 0.00 to 1.0) based on the MAX value
$color = $row['pop_inf'] / MAX; 
// now we translate that to an 8-bit hexadecimal value
$color = (int) (256 * $color) - 1;
// pad it to a 2 digit hex string
$color = sprintf("%02x", base_convert($color, 10, 16));
// Now you can print a full hex value color as 0x000000 - 0xff0000
$color = "$color0000";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM