Reputation:
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.
Upvotes: 0
Views: 1498
Reputation: 710
<?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.
Upvotes: 0
Reputation: 6003
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>";
}
Upvotes: 1
Reputation: 57322
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
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
Upvotes: 3
Reputation: 1971
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>";
}
Upvotes: 0
Reputation: 74036
Why don't you just use CSS with the nth-child
selector?
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.
Besides the IE, most browsers support this. See the Browser compatibility of MDN.
Upvotes: 11
Reputation: 1802
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>";
}
Upvotes: 0
Reputation: 630
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.
Upvotes: 0
Reputation: 2790
for ($year=2013; $year<=2023; $year++)
{
echo "<tr bgcolor='".$year%2==0?"#fff":"#000"."'><td>$year</td><td>$tdate</td></tr>";
}
Upvotes: 1