Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.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
php7和jQuery中的状态栏_Php_Jquery - Fatal编程技术网

php7和jQuery中的状态栏

php7和jQuery中的状态栏,php,jquery,Php,Jquery,我正在使用已按百分比格式化的状态栏。我目前正在jQuery中使用PHP7.1和状态栏 状态栏1至25为蓝色,25至50为绿色,50至75为橙色,75至100为红色 我想做很多“如果和其他”或“开关和案例”,我发现检查许多变量是疯狂的 我调查了这里的社区,什么也没发现 有没有一个聪明的方法可以做到这一点 我必须使用jQuery和PHP 多谢各位 <?php $limit=3000; $used=1200; $percentage = $used / $limit *

我正在使用已按百分比格式化的状态栏。我目前正在jQuery中使用PHP7.1和状态栏

状态栏1至25为蓝色,25至50为绿色,50至75为橙色,75至100为红色

我想做很多“如果和其他”或“开关和案例”,我发现检查许多变量是疯狂的

我调查了这里的社区,什么也没发现

有没有一个聪明的方法可以做到这一点

我必须使用jQuery和PHP

多谢各位

<?php
    $limit=3000;
    $used=1200;
    $percentage = $used / $limit * 100;
    $percentage = round($percentage, 0)
?>

<script type="text/javascript">
    $(document).ready(function(){
    $('#bar').barfiller({ barColor: '#0080ff' });
    });
</script>

<div id="bar" class="barfiller">
<div class="tipWrap">
<span class="tip"></span>
</div>
<span class="fill" data-percentage="<?php echo $percentage; ?>"></span>
</div>

$(文档).ready(函数(){
$('#bar').barfiller({barColor:'#0080ff'});
});

您可以保留一组颜色,如

const colors = ['#faa', '#afa', '#cfc', '#afd'];

const statusBar = $('.fill');
let index = parseInt(parseInt(statusBar.text()) / 25);
if (index == colors.length) {
    index--;
}
statusBar.css('background', colors[index]);

PHP相当于@subbasis his answer“


这听起来像是您正在或计划从其他地方获取数据,预期的数据源是什么?或者我错了吗?不使用if/else或switch语句,您可以这样做:
$barColor=in_数组($percentage,range(0,25))?“#0000FF:”;
$barColor=in_数组($percentage,range(26,50))?“#00FF00”:$barColor;
,您计划使用的每种颜色都需要一行。答案是JSON已经格式化,我只需要找到一个聪明的解决方案。这可能也是一个JSON问题。我不清楚这个问题,我对JS/JSON没有任何帮助。@FunkFortyNiner这个问题已经用这种格式了,不需要做任何事情东西只是想要比较。你必须将百分比减少为1。因为如果百分比为100,程序将尝试获取数组中的第四个元素。谢谢你的建议。@JeroenGroenveld我喜欢你的想法,它很有效,非常感谢:-)php不会刷新进度条%,只能在js@LeoTahk如果你在提供的代码OP中,百分比是在加载页面时计算的,因此在Javascript或PHP中计算条形图颜色实际上并不重要。在这两种情况下,只有在重新加载页面时才会更新。
$colors = ['#faa', '#afa', '#cfc', '#afd'];
$barColor = $colors[intval(($percentage == 100 ? --$percentage : $percentage) / 25)];