简体   繁体   English

尝试在PHP中替换行颜色

[英]Trying to Alternate Row Color in PHP

I'm looking to alternate row colors using PHP function. 我正在寻找使用PHP函数替换行颜色的方法。 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. 如果均匀,则为黑色。

Why don't you just use CSS with the nth-child selector? 为什么不将CSS与nth-child选择器一起使用?

tr:nth-child( 2n ) {
  background-color: #000;
}
tr:nth-child( 2n + 1 ) {
  background-color: #FFF;
}

Then no further attributes are needed on the <tr> element. 然后,在<tr>元素上不需要其他属性。

Besides the IE, most browsers support this. 除了IE,大多数浏览器都支持此功能。 See the Browser compatibility of MDN . 请参阅MDN浏览器兼容性

From W3c 从W3c

15.1.1 Background color - bgcolor attribute has been deprecated in favor of style sheets for specifying background color information. 15.1.1背景颜色-不建议使用bgcolor属性,而应使用样式表来指定背景颜色信息。

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 例如,赋值运算符所做的就是将右侧值赋给左手

在此处输入图片说明

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. 在大多数情况下,所有PHP变量都只有一个作用域。 This single scope spans included and required files as well. 这个单一范围也涵盖了包含和必需的文件。

above is quoted from php manual to read more check this 以上摘自PHP手册引述阅读更多检查此

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

   return $color;
}

for ($year=2013; $year<=2023; $year++) 
    {
    $color = row($year);
    echo "<tr bgcolor='$color'><td>$year</td><td>$tdate</td></tr>";
    }
for ($year=2013; $year<=2023; $year++) 
{
    echo "<tr bgcolor='".$year%2==0?"#fff":"#000"."'><td>$year</td><td>$tdate</td></tr>";
}

You aren't saving the result of your function anywhere. 您不会在任何地方保存函数的结果。 Try this: 尝试这个:

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

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

It's about variable scope. 关于可变范围。

you are not returning anything from function,do like this 你没有从函数返回任何东西,这样做

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

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

Just like other programming languages, you have to know when you're working with local variables and globals. 就像其他编程语言一样,您必须知道何时使用局部变量和全局变量。 In this case, you're trying to use a variable in one function that is local to another. 在这种情况下,您试图在一个函数中使用另一个函数的局部变量。

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

for ($year=2013; $year<=2023; $year++) 
    {
    echo "<tr bgcolor='".row($year)."'><td>$year</td><td>$tdate</td></tr>";
    }
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$dbname=""; // Database name
$tblname=""; // Table name
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$sql="SELECT * FROM $tblname";
$result=mysql_query($sql);
// Define $color=1
$color="1";
echo '<h3 align = "center">Employee Details <hr /></h3>';
echo '<table width="400" border="1" align="center" cellpadding="2" cellspacing="0">';
while($rows=mysql_fetch_row($result)){
// If $color==1 table row color = #FFCCFF
if($color == 1){
echo "<tr bgcolor='#FFCCFF'><td>$rows[0]</td><td>$rows[1]</td><td>$rows[2]</td><td>$rows[3]</td></tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, table row color = #FFC600
else {
echo "<tr bgcolor='#FFC600'><td>$rows[0]</td><td>$rows[1]</td><td>$rows[2]</td><td>$rows[3]</td></tr>";
// Set $color back to 1
$color="1";
}
}
echo '</table>';
mysql_close();
?>

In the above coding,first we select the data from the database and then we define a variable $color with the value 1. After while loop an if condition is added.If $color=1 then table row color will be #FFCCFF and inside the if condition we set the $color==2 for switching to other color.Now when $color not equal to 1 then table row color= #FFC600 and else condition executes.Under the else condition we again set the $color back to 1. 在上面的编码中,首先我们从数据库中选择数据,然后定义一个值为$ 1的变量$ color。在while循环中添加了if条件。如果$ color = 1,则表行颜色将为#FFCCFF,并且在内部如果if条件我们设置$ color == 2切换到其他颜色。现在,当$ color不等于1时,则表行color =#FFC600并执行else条件。在else条件下,我们再次将$ color重新设置为1 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM