Looping in PHP and changing color... (hex codes?)

Posted on 2008-10-11
Last Modified: 2012-05-05
This might not be possible: I am looping through a data set in PHP, and I want to display a status message to the user. I was hoping to make this message start off really light, and get darker as it nears completion. Preferably a light green to a dark green. So my question is, is there a simple way to do this via css? Could I use a hexcode and adjust the number somehow? I know I could just do a thousand if/else statements, but I would rather not.
Question by:interclubs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 27

Expert Comment

ID: 22696254
If you are looping through in PHP it would seem that you could just have the desired hex codes in an array and select a different color during each iteration.


in your loop, using $i as the iteration var:

The use $colortouse for your color within your HTML tags.

Author Comment

ID: 22696321
That would work, the only issue is I don't know how many items are in the loop, it changes based on the size of the array. It could have 5 elements, or it could have 60.
LVL 11

Accepted Solution

AlexanderR earned 500 total points
ID: 22696347
Credit goes to

If you dont necessarily want to go from lightest to brightest, but stop wherever it stops, then change

$members = count($data);
$interval = round(100/$members);
echo $interval;
for($i=1;$i<=100;$i += $interval){

to simply
or whatever other interval you want,

Hope this does it.
function hexDarker($hex,$factor = 30)
        $new_hex = '';
        $base['R'] = hexdec($hex{0}.$hex{1});
        $base['G'] = hexdec($hex{2}.$hex{3});
        $base['B'] = hexdec($hex{4}.$hex{5});
        foreach ($base as $k => $v)
                $amount = $v / 100;
                $amount = round($amount * $factor);
                $new_decimal = $v - $amount;
                $new_hex_component = dechex($new_decimal);
                if(strlen($new_hex_component) < 2)
                        { $new_hex_component = "0".$new_hex_component; }
                $new_hex .= $new_hex_component;
        return $new_hex;        
$original = "90EE90";
$data = array('1','2','3');
$members = count($data);
$interval = round(100/$members);
echo $interval;
for($i=1;$i<=100;$i += $interval){
$newCol = hexDarker($original, $i);
echo '<td bgcolor="'.$newCol.'">h</td>';

Open in new window

LVL 44

Expert Comment

ID: 22696402
If you are coding PHP, it can only deliver to the browser one data set at a time.  Are you trying to send 60 page refreshes to the browser in real time, just to show some progress that the PHP file is going through?

This seems a little ridiculous to me, the PHP file mght take 0.1 seconds to complete 100 iterations, but 100 seconds to send 10 page refreshes to the browser.  So what are you accomplishing? Massive delays in showing some progress that finished on the server code before the first page even refreshed ????
LVL 27

Expert Comment

ID: 22697046
interclubs, you might consider this function to select a certain color from within an array of colors that you define.
function getcolor($pass,$passes){
  global $colors;
  return $colors[$color];
// test record counts...
for ($i = 0; $i < count($record_counts); $i++) {
  echo "record count: $record_counts[$i]<br>";
  for ($j = 0; $j < $record_counts[$i]; $j++) {
    echo "  pass $j color = ".getcolor($j,$record_counts[$i])."<br>";
  echo "<hr>";

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSS question 4 57
Ajax success not firing alert 6 45
Selenium cssSelector and attribute 5 14
Firefox CSS Help. 1 14
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question