Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-11
5
Medium Priority
?
346 Views
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.
0
Comment
Question by:interclubs
5 Comments
 
LVL 27

Expert Comment

by:ddrudik
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.

$colorarray=array('#F5FF80','#99FF80','#80FFFF');

in your loop, using $i as the iteration var:
$colortouse=$colorarray[$i];

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

Author Comment

by:interclubs
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.
0
 
LVL 11

Accepted Solution

by:
AlexanderR earned 2000 total points
ID: 22696347
Credit goes to
http://www.weberdev.com/get_example-3641.html

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
for($i=1;$i<=100;$i++)
or whatever other interval you want,

Hope this does it.
<?php
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');
?>  
 
<table>
<tr>
</tr>
<?php
$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>';
}
?>
</table>

Open in new window

0
 
LVL 44

Expert Comment

by:scrathcyboy
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 ????
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22697046
interclubs, you might consider this function to select a certain color from within an array of colors that you define.
<pre>
<?php
$colors=array('#000000','#000020','#000040','#000060','#000080','#0000A0','#0000C0','#0000E0','#0000FF');
function getcolor($pass,$passes){
  global $colors;
  $color=floor(($pass/$passes)*count($colors));
  if($color<0){
    $color=0;
  }
  if($color>count($colors)-1){
    $color=count($colors)-1;
  }
  return $colors[$color];
}
// test record counts...
$record_counts=array(1,4,9,13,24);
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

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

783 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