duanfei1975 2013-02-01 16:39
浏览 46
已采纳

尝试在PHP中交替行颜色

I'm looking to alternate row colors using PHP function. Here's what I have (although it does not work):

function row($year) {
    if($year%2) 
        $color == "#FFF";
    else
        $color == "#000";
}

for ($year=2013; $year<=2023; $year++) 
    {
    row($year);
    echo "<tr bgcolor='$color'><td>$year</td><td>$tdate</td></tr>";
    }

Basically, if a year is odd I would like the color of the row to be white. If even, black.

  • 写回答

8条回答 默认 最新

  • dongyi2159 2013-02-01 16:43
    关注

    From W3c

    15.1.1 Background color - bgcolor attribute has been deprecated in favor of style sheets for specifying background color information.

    now what should you do is

    function row($year) {
        return ($year % 2 == 0) ? "#FFFFFF" : "#000000";
    }
    
    for ($year = 2013; $year <= 2023; $year++) {
        echo "<tr style='background-color:".row($year).";'><td>$year</td><td>$tdate</td></tr>";
    }
    

    however its looks like you are not aware of what == does its a equal to operator its not assignment operator

    what assignment operator do is assign right hand side value to left hand for example

    enter image description here

    what Comparison Operators(==) do is

    $a == $b    Equal   TRUE if $a is equal to $b after type juggling.
    

    second you also there is the scope of a variable

    The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

    above is quoted from php manual to read more check this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证