alternating row colors on query generated table?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-07-2004
LRW
 
Posts: n/a
Default alternating row colors on query generated table?

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>&nbsp;--&nbsp;".$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


Reply With Quote
  #2 (permalink)  
Old 03-07-2004
Mike Peters
 
Posts: n/a
Default Re: alternating row colors on query generated table?

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>&nbsp;--&nbsp;".$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>&nbsp;--&nbsp;".$row_RSwho['email']."</td></tr>";
}
}

--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Reply With Quote
  #3 (permalink)  
Old 03-07-2004
Pedro Graca
 
Posts: n/a
Default Re: alternating row colors on query generated table?

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>&nbsp;--&nbsp;".$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 =--
Reply With Quote
  #4 (permalink)  
Old 03-07-2004
LRW
 
Posts: n/a
Default Re: alternating row colors on query generated table?

"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>&nbsp;--&nbsp;".$row_RSwho['email']."</td></tr>";
> }
> }


Excellent! Perfect! Works prefectly. =)
Thanks you! (Oh...I can do so much with this....)
Thanks! =)
Liam


Reply With Quote
  #5 (permalink)  
Old 03-08-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: alternating row colors on query generated table?

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>&nbsp;--&nbsp;".$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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:16 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0