changing row colors

Discussion in 'PHP' started by mnymkr, Oct 13, 2007.

  1. #1
    I am dumping a database table....it has 4 columns and about 260 rows


    i would like for the each row in a set of 3 to be a different shade of grey


    row1 - grey
    row2 - light grey
    row3 - darkgrey

    repeat


    what the best way to accomplish this?
     
    mnymkr, Oct 13, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    function get_color()
    {
    	static $current = 0;
    	static $colors = array(
    		'#666666',
    		'#999999',
    		'#CCCCCC'
    	);
    
    	return $colors[$current++ % sizeof($colors)];
    }
    
    
    PHP:
    Set the colors in the function itself. As many as you want.

    And then in your loop just do:

    
    
    while (/* condition */)
    {
        $background = get_color();
    
        // Do whatever with background
    }
    
    PHP:
     
    nico_swd, Oct 13, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    that is amazing

    you know i know so crazy things, like how to grow alumina nanotube and attach proteins but sometimes the elegance in php amazes me


    thank you thank you

    and teach me teach me LOL
     
    mnymkr, Oct 13, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    It was an interesting programming experience for me too. :) I was trying to keep the function as short as possible.

    It took me a moment to think of using the modulus operator.
     
    nico_swd, Oct 13, 2007 IP
  5. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #5
    yeah that is really cool. worked perfectly
     
    mnymkr, Oct 15, 2007 IP