Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php html生成条形图以正确显示百分比?_Php_Html_Graph - Fatal编程技术网

使用php html生成条形图以正确显示百分比?

使用php html生成条形图以正确显示百分比?,php,html,graph,Php,Html,Graph,有人能帮我吗?我正在尝试用php和html编写一个简单的代码,我的目标是通过数据库获取我的变量,并在最后添加一个%的数字,但我的代码让我感到困惑 最后一个可以工作,但它是硬编码的 点击这里查看图片。 黄色的图表我将以55%的比例显示,其余的将不会填写,如底部的图表 <style type="text/css"> .red {background-color: red;} .green {background-color: green;} .yellow{bac

有人能帮我吗?我正在尝试用php和html编写一个简单的代码,我的目标是通过数据库获取我的变量,并在最后添加一个%的数字,但我的代码让我感到困惑

最后一个可以工作,但它是硬编码的

点击这里查看图片。 黄色的图表我将以55%的比例显示,其余的将不会填写,如底部的图表

<style type="text/css">
    .red {background-color: red;}
    .green {background-color: green;}
    .yellow{background-color: yellow;}
    .bar { width: 15%; border: 1px solid #000; background: grey; }
    </style>


    <?php   
    if ($percentage >= 51 && $percentage <= 74) {
    echo "<div class=\"bar\" align=\"left\"><div class=\"yellow\" style=\"width: $percentage %\">$percentage test</div></div>";
    } else if ($percentage >= 75){
    echo "<div class=\"bar\" align=\"left\"><div class=\"green\" style=\"width: $percentage \"%\">$percentage test</div></div>";
    } else if ($percentage >= 0 && $percentage <= 50) {
    echo "<div class=\"bar\" align=\"left\"><div class=\"red\" style=\"width:46%\">$percentage test</div></div>";
    }
    ?>

.red{背景色:红色;}
.green{背景色:绿色;}
.yellow{背景色:黄色;}
.bar{宽度:15%;边框:1px实心#000;背景:灰色;}

您不能像这样将变量放入字符串中。所以你应该这样做:

if ($percentage >= 51 && $percentage <= 74)
    echo "<div class=\"bar\" align=\"left\"><div class=\"yellow\" style=\"width:". $percentage." %\">".$percentage." test</div></div>";

如果($percentage>=51&&$percentage我看不出你是如何得到百分比值的,但我可以看到你用3种不同的方式定义你的风格:
第一个会产生:width:33 px;这在某种程度上是正确的,但第二个是错误的,正确的是:

echo "<div class=\"bar\" align=\"left\"><div class=\"red\" style=\"width:$percenage%\">$percentage test</div></div>";
echo“$percentage test”;

echo“$percentage test”;
这是为了完成上述答案。 我忘了你也可以用!重要的论点。
…宽度:$percantage%!重要;

还是没什么,我想如果它放在桌子上可能会有用?是的,我确实对stackoverflow感到抱歉。
echo "<div class=\"bar\" align=\"left\"><div class=\"red\" style=\"width:".$percentage."%\">$percentage test</div></div>";