1

I have a script that counts the amount of items in a folder, the script looks like this, which I found 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";
?>

When displayed on a webpage I get the amount of items in the folder next to some text like this:

Folder 1: 12 items

The items vary, and I would like for the numbers to change color based on their value.

I don't have much knowledge regarding php. I found this script here and it seems to be what I need:

<?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>";
?>

I've tried changing the $v to $i, but I don't get it, and since I'm such a noob, I figured I'll ask the experts over at stackoverflow, so guys, could you help me out here? :)

Here is what my .php looks like:

<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>
Community
  • 1
  • 1
Remi
  • 75
  • 1
  • 2
  • 13
  • First off you don't have error reporting on. Change `$v` to `$i` and put the if/elseif/elseif/echo under the `` tag – Daan May 27 '14 at 07:56

1 Answers1

2

Put the color script under counting script and change $v to $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>

You might want to adjust the ranges for $i

Ladislav Gallay
  • 478
  • 6
  • 15