Dynamic Table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adilmarwat2004
Forum Commoner
Posts: 44
Joined: Fri Sep 04, 2009 11:28 pm

Dynamic Table?

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Dynamic Table?

Post 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
}
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Dynamic Table?

Post by pbs »

Try this

Code: Select all

 
<?php
    $k = 1;
    for()
    {
 
        if ($k % 2 == 0)
            $color = "blue";
        else
            $color = "white";
        
        $k++;
    }
?>
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Dynamic Table?

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 5:14 pm, edited 1 time in total.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Dynamic Table?

Post 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>";
?>
Post Reply