PHP:根据$ i更改文本颜色

时间:2014-05-27 07:53:33

标签: php colors

我有一个脚本来计算文件夹中的项目数量,脚本看起来像这样,我找到了here

<?php 
    // integer starts at 0 before counting
    $i = 0; 
    $dir = 'folder1/images/';
    if ($handle = opendir($dir)) {
        while (($file = readdir($handle)) !== false){
            if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
                $i++;
        }
    }
    // prints out how many were in the directory
    echo "$i items";
?>

当在网页上显示时,我会在文件旁边的文件夹中获得这样的项目数量,如下所示:

文件夹1:12件

项目各不相同,我希望数字根据其值改变颜色。

我对PHP没有太多了解。我找到了这个脚本here,它似乎就是我需要的:

<?php
$color = "#fff";

if (($v >= 0) && ($v <= 9))
   $color = "#E54028";
else if (($v >= 9) && ($v <= 15))
   $color = "#F18D05";
else if ($v >= 15)
   $color = "#61AE24";

echo "<span style=\"color: $color\">12</span>";
?>

我已经尝试将$ v改为$ i,但我没有得到它,而且因为我是这样的菜鸟,我想我会问专家们stackoverflow,伙计们,你能帮助我吗? :)

这是我的.php的样子:

<div id="body">
<?php
$color = "#000000";

if (($v >= 0) && ($v <= 9))
   $color = "#E54028";
else if (($v >= 9) && ($v <= 15))
   $color = "#F18D05";
else if ($v >= 15)
   $color = "#61AE24";

echo "<span style=\"color: $color\">Text</span>";

?>
<ul>
    <li>Folder 1:<strong>
    <?php 
        // integer starts at 0 before counting
        $i = 0; 
        $dir = 'folder1/images/';
        if ($handle = opendir($dir)) {
            while (($file = readdir($handle)) !== false){
                if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
                    $i++;
            }
        }
        // prints out how many were in the directory
        echo "$i items";
    ?>
    </strong></li>
</ul>
</div>

1 个答案:

答案 0 :(得分:2)

颜色脚本放在计算脚本下,并将$v更改为$i

<div id="body">
<ul>
    <li>Folder 1:<strong>
    <?php 
        // integer starts at 0 before counting
        $i = 0; 
        $dir = 'folder1/images/';
        if ($handle = opendir($dir)) {
            while (($file = readdir($handle)) !== false){
                if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
                    $i++;
            }
        }

        $color = "#000000";

        if (($i >= 0) && ($i <= 9))
           $color = "#E54028";
        else if (($i >= 9) && ($i <= 15))
           $color = "#F18D05";
        else if ($i >= 15)
           $color = "#61AE24";

        // prints out how many were in the directory
        echo "<span style=\"color: $color\">$i items</span>";
    ?>
    </strong></li>
</ul>
</div>

您可能需要调整$i

的范围