andy
andy

Reputation: 85

How to color even/odd table rows in PHP

OK I have a question I have this code which I will list below. I need to make the even rows a light blue and the odd a white. Now it doesnt show up so I am assume I am doing soemthing wrong. Now do I need to do the order in it so that way the rows will look the way I need it to do?

<html>
<head>

<title> Html Tables</title>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
  if ($rowcount%2==0){
   echo "</tr>";
   echo "<tr bgcolor=\"#5CCDC9\">\n";
   }
   else{
    echo "</tr>";
    echo "<tr bgcolor=\"#FFFFFF\">\n";
    }
    $rowscount++;
   }
 }
echo "</tr>";
echo "</table>";
 ?>
</body>
</html>

ok I am trying to understand this better after reading a few things this is my new code

<html>
<head>

<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
 if ($rowcount%2==0){
    echo "</tr>";
   echo "<tr class=\"even\">\n";
   }
   else{
    echo "</tr>";
    echo "<tr class=\"odd\">\n";
    }
    $rowcount++;
   }
 }
 echo "</tr>";
 echo "</table>";
 ?>
 </body>
 </html>

now i just dont understand how to write it in PHP I am reading and trying to figure out how to make sense of it. Sorry I am a newbie at this.

Upvotes: 1

Views: 5610

Answers (4)

AlexV
AlexV

Reputation: 23098

Try this. It's a little bit cleaner and it works:

<html>
    <head>
        <title> Html Tables</title>
    </head>
    <body>
        <table width="50%" cellpadding="2" cellspacing="2" border="1">
<?php
for($x = 1; $x <= 12; $x++) {
    if ($x % 2 == 0) {
        echo '            <tr bgcolor="#5CCDC9">', PHP_EOL;
    } else {
        echo '            <tr bgcolor="#FFFFFF">', PHP_EOL;
    }
    echo '                <td align="center" style="width:100px">' . $x . '</td>', PHP_EOL;
    echo '            </tr>', PHP_EOL;
}
?>
        </table>
    </body>
</html>

Upvotes: 1

James Allardice
James Allardice

Reputation: 165971

The easiest way to do this is with CSS. You can use the nth-child rule to select odd and even rows of the table and colour them differently. That way you don't need the modulus operator if statement you're using.

See this fiddle for an example.

Upvotes: 2

Andrew Curioso
Andrew Curioso

Reputation: 2191

You are using $rowcount in your conditional and initialization but you are using $rowscount (with in "s") in your incrementing.

Note: you should really be using CSS for that rather than the bgcolor property.

Upvotes: 5

AJ.
AJ.

Reputation: 28184

you have a typo...change $rowscount++ to $rowcount++

Upvotes: 3

Related Questions