This is a discussion on alternating row colors on query generated table? within the PHP Language forums, part of the PHP Programming Forums category; Is there some way to make a table have alternating colors for rows when you're generating the table data ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Is there some way to make a table have alternating colors for rows when
you're generating the table data with a WHILE statement? You know, row 1 has a gray BG, row 2 is white, row 3 is gray, 4 is white, etc, for as long as there's data. Here's what I'm using right now: $query_RSwho = "SELECT * FROM tbl_indv ORDER BY name ASC"; $RSwho = @mysql_query($query_RSwho, $connection) or die("Couldn't query: " .. mysql_error()); while ($row_RSwho = mysql_fetch_array($RSwho)) { if ($row_RSwho['name'] != "X") { $names .= "<tr align=left><td width=120>".$row_RSwho['name']."</td><td align=left> -- ".$row_RSwho ['email']."</td></tr>"; } } So, in that WHILE section, is there a way to alternate bgcolor table cell tags? If there's a web site, or something, just point me? =) Thanks! Liam |
|
|||
|
On 2004-03-07, LRW wrote:
> Is there some way to make a table have alternating colors for rows when > you're generating the table data with a WHILE statement? > You know, row 1 has a gray BG, row 2 is white, row 3 is gray, 4 is white, > etc, for as long as there's data. > Here's what I'm using right now: > > $query_RSwho = "SELECT * FROM tbl_indv ORDER BY name ASC"; > $RSwho = @mysql_query($query_RSwho, $connection) or die("Couldn't query: " > . mysql_error()); > while ($row_RSwho = mysql_fetch_array($RSwho)) { > if ($row_RSwho['name'] != "X") { > $names .= "<tr align=left><td > width=120>".$row_RSwho['name']."</td><td > align=left> -- ".$row_RSwho ['email']."</td></tr>"; > } > } > > So, in that WHILE section, is there a way to alternate bgcolor table cell > tags? > If there's a web site, or something, just point me? =) > Thanks! > Liam > I'd do it something like this: $i = 1; while ($row_RSwho = mysql_fetch_array($RSwho)) { if ($row_RSwho['name'] != "X") { $bgcolour = ($i%2) ? 'bgcolor=\'white\'' : 'bgcolor=\'grey\''; ++$i; $names .= "<tr align=left " . $bgcolour . "><td width=120>".$row_RSwho['name']."</td><td align=left> -- ".$row_RSwho['email']."</td></tr>"; } } -- Mike Peters mike [-AT-] ice2o [-DOT-] com http://www.ice2o.com |
|
|||
|
LRW wrote:
> Is there some way to make a table have alternating colors for rows when > you're generating the table data with a WHILE statement? > You know, row 1 has a gray BG, row 2 is white, row 3 is gray, 4 is white, > etc, for as long as there's data. > Here's what I'm using right now: > > $query_RSwho = "SELECT * FROM tbl_indv ORDER BY name ASC"; > $RSwho = @mysql_query($query_RSwho, $connection) or die("Couldn't query: " > . mysql_error()); > while ($row_RSwho = mysql_fetch_array($RSwho)) { > if ($row_RSwho['name'] != "X") { > $names .= "<tr align=left><td > width=120>".$row_RSwho['name']."</td><td > align=left> -- ".$row_RSwho ['email']."</td></tr>"; > } > } > > So, in that WHILE section, is there a way to alternate bgcolor table cell > tags? > If there's a web site, or something, just point me? =) > Thanks! > Liam > > Try this: <?php $i = 0; $j = 20; while (--$j) { $i = 1 - $i; // 1, 0, 1, 0, 1, 0, 1, 0, ... echo 'class="', ($i)?('odd'):('even'), '"', "\n"; } ?> Adapt to your liking. -- --= my mail box only accepts =-- --= Content-Type: text/plain =-- --= Size below 10001 bytes =-- |
|
|||
|
"Mike Peters" <o0__mike__0oREMOVE@THIShotmail.com> wrote in message
news:b33215d8a31036ae100d029455a75101@news.teranew s.com... > I'd do it something like this: > > $i = 1; > while ($row_RSwho = mysql_fetch_array($RSwho)) { > if ($row_RSwho['name'] != "X") { > $bgcolour = ($i%2) ? 'bgcolor=\'white\'' : 'bgcolor=\'grey\''; > ++$i; > $names .= "<tr align=left " . $bgcolour . "><td > width=120>".$row_RSwho['name']."</td><td > align=left> -- ".$row_RSwho['email']."</td></tr>"; > } > } Excellent! Perfect! Works prefectly. =) Thanks you! (Oh...I can do so much with this....) Thanks! =) Liam |
|
|||
|
On 2004-03-07, LRW <druid@NOSPAHMcelticbear.com> wrote:
> Is there some way to make a table have alternating colors for rows when > you're generating the table data with a WHILE statement? > You know, row 1 has a gray BG, row 2 is white, row 3 is gray, 4 is white, > etc, for as long as there's data. > Here's what I'm using right now: > > $query_RSwho = "SELECT * FROM tbl_indv ORDER BY name ASC"; > $RSwho = @mysql_query($query_RSwho, $connection) or die("Couldn't query: " > . mysql_error()); > while ($row_RSwho = mysql_fetch_array($RSwho)) { > if ($row_RSwho['name'] != "X") { > $names .= "<tr align=left><td > width=120>".$row_RSwho['name']."</td><td > align=left> -- ".$row_RSwho ['email']."</td></tr>"; > } > } > > So, in that WHILE section, is there a way to alternate bgcolor table cell > tags? > If there's a web site, or something, just point me? =) There was recently a nice article on this: http://www.alistapart.com/articles/zebratables/ -- http://home.mysth.be/~timvw |