Alternate table row color using CSS?

后端 未结 9 1866
囚心锁ツ
囚心锁ツ 2020-11-22 09:03

I am using a table with alternate row color with this.

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 09:37

    There is a fairly easy way to do this in PHP, if I understand your query, I assume that you code in PHP and you are using CSS and javascript to enhance the output.

    The dynamic output from the database will carry a for loop to iterate through results which are then loaded into the table. Just add a function call to the like this:

    echo "";  //this calls the function based on the iteration of the for loop.
    

    then add the function to the page or library file:

    function getbgc($trcount)
    {
    
    $blue="\"background-color: #EEFAF6;\"";
    $green="\"background-color: #D4F7EB;\"";
    $odd=$trcount%2;
        if($odd==1){return $blue;}
        else{return $green;}    
    

    }

    Now this will alternate dynamically between colors at each newly generated table row.

    It's a lot easier than messing about with CSS that doesn't work on all browsers.

    Hope this helps.

提交回复
热议问题