Page 1 of 1

Dynamic Table?

Posted: Thu Feb 18, 2010 9:11 pm
by adilmarwat2004
I want to create a dynamic table in php that it 1st row color is blue and 2nd row color is white and so on.

How it is possible.

Adil

Re: Dynamic Table?

Posted: Thu Feb 18, 2010 9:57 pm
by requinix

Code: Select all

color = blue
for each row in table {
    print row with color
 
    if color == blue then color = white else color = blue
}

Re: Dynamic Table?

Posted: Thu Feb 18, 2010 11:08 pm
by pbs
Try this

Code: Select all

 
<?php
    $k = 1;
    for()
    {
 
        if ($k % 2 == 0)
            $color = "blue";
        else
            $color = "white";
        
        $k++;
    }
?>
 

Re: Dynamic Table?

Posted: Thu Feb 18, 2010 11:18 pm
by McInfo
There is also the CSS3 :nth-child() pseudo-class selector.

Code: Select all

/* CSS */
tr:nth-child(odd) {
    background-color: blue;
}
tr:nth-child(even) {
    background-color: white;
}
The PHP solution is more compatible with older browsers, though.

Edit: This post was recovered from search engine cache.

Re: Dynamic Table?

Posted: Thu Feb 18, 2010 11:34 pm
by Weiry

Code: Select all

<?php
print "<table>";
for($i=0; $i<4; $i++){
    $color = ($i % 2) ? "white" : "blue"; 
    print "<tr><td>{$color}</tr></td>";
}
print "</table>";
?>