I have an array of float values in PHP and I want to turn the array into a bar chart using CSS. Each float value represents a percentage, so if a value is 50.0 then the bar should be 50% of a certain height. I am new to CSS, so I was wondering if anyone could show me how I would go about doing this.
I believe it should look something like this. I doubt that this code would work, but it's an example.
<?php
$values = array(50.0, 20.0, 30.0, 45.0); //This is the array of percentage values
?>
<style>
<?php
for ($i = 0; $i < count($values); $i++) //Create a new bar for each value
{?>
#bar
{
height: <?php 300 * ($values[$i] / 100.0); ?> px; //Specify the height of each bar
width: 30px;
}
<?php
}?>
</style>
Edit:
I've added the code suggested by Tom, but my web page currently produces no bars. Here is the full code for my PHP file. Why does it not produce any CSS content?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Visualisation result</title>
<style>
.bar
{
color: #00ff21;
width: 30px;
}
</style>
</head>
<body>
<?php
$values = array(50.0, 20.0, 30.0, 45.0);
for ($i = 0; $i < count($values); $i++)
{
$currentHeight = 300 * $values[$i] / 100.0; ?>
<div class="bar" style="height: <?php echo($currentHeight); ?> "></div>
<?php
}
?>
</body>
</html>