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 12 2731
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****************@THIShotmail.com> wrote in message
news:b3******************************@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 <dr***@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
In message <rMM2c.79807$ko6.426465@attbi_s02>, LRW
<dr***@NOSPAHMcelticbear.com> writes "Mike Peters" <o0****************@THIShotmail.com> wrote in message news:b3******************************@news.terane ws.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
The above is how I would have done it, except that I would use CSS & a
class to set the background so I can change it later to suit without
having to fiddle with the PHP - one day I might want alternate pale
green & pale pink - you never know!
--
Five Cats
Email to: cats_spam at uk2 dot net
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>"; } }
I personally don't like counters. I use something like this:
//At top of file
$bgcolor1 = '#FF0000';
$bgcolor2 = '#00FF00';
//where you want your table to go
$bgcolor='';
while ($foo = mysql_fetch_array($result)) {
$bgcolor = ($bgcolor == $bgcolor1) ? $bgcolor2: $bgcolor1;
echo '<tr><td bgcolor="'.$bgcolor.'">stuff</td></tr>';
}
Or use a CSS class using the same method. I find the code is tidier than using
a counter. I imagine it's slower than a counter because it has to compare
strings every loop. But unless you have gigantic tables you probably won't
notice a difference.
Regards,
Shawn
--
Shawn Wilson sh***@glassgiant.com http://www.glassgiant.com
Five Cats wrote: In message <rMM2c.79807$ko6.426465@attbi_s02>, LRW <dr***@NOSPAHMcelticbear.com> writes"Mike Peters" <o0****************@THIShotmail.com> wrote in message news:b3******************************@news.teran ews.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
The above is how I would have done it, except that I would use CSS & a class to set the background so I can change it later to suit without having to fiddle with the PHP - one day I might want alternate pale green & pale pink - you never know!
Well, that's a good idea! But...how do you do that in a CSS?
Oh! Instead of 'bgcolor=\'a_color\' use the class tag in that where/if?
Liam
LRW <dr***@NOSPAHMcelticbear.com> wrote in
news:z893c.506350$I06.5412308@attbi_s01: Five Cats wrote:
$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>"; } } The above is how I would have done it, except that I would use CSS & a class to set the background so I can change it later to suit without having to fiddle with the PHP - one day I might want alternate pale green & pale pink - you never know!
Well, that's a good idea! But...how do you do that in a CSS? Oh! Instead of 'bgcolor=\'a_color\' use the class tag in that where/if?
Class *attribute*. Yep, change "bgcolor='white'" to "class='odd'" and
"bgcolor='grey'" to "class='even'". Then in your stylesheet set
tr.odd {background-color: white;}
tr.even {background-color: grey;}
One advantage is that you can do other things than just changing colors.
If you wanted to, you could make odd and even rows have different heights,
for example.
"LRW" <dr***@NOSPAHMcelticbear.com> writes: Is there some way to make a table have alternating colors for rows when you're generating the table data with a WHILE statement?
Set an array of two values on indexes 0 and 1.
Start iterating over the query rows with a counter that steps by 1.
Use a modulus 2 operation to select from the array.
$colors = array('red', 'blue');
$i = 0;
while(have-some-rows)
{
print $colors[$i % 2];
$i ++;
}
HTH
--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
In article <yvJ2c.497761$I06.5347103@attbi_s01>,
"LRW" <dr***@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? =) Thanks! Liam
I like compact solutions:
while ($row_RSwho = mysql_fetch_array($RSwho)) {
$bg = $nr++ % 2 ? "ffffff" : "eeeeee";
print "<tr bgcolor='#$bg'>....";
}
But as others have said - use stylesheets:
while ($row_RSwho = mysql_fetch_array($RSwho)) {
$bg = $nr++ % 2 ? "even" : "odd";
print "<tr class='$bg'>....";
}
--
Sandman[.net]
While the city slept, LRW <dr***@NOSPAHMcelticbear.com> feverishly typed: 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.
[...]
I've just quickly knocked this up: http://www.nigenet.org.uk/stuff/phpRowColours.php
Hope that helps.
Cheers,
Nige
--
Nigel Moss.
Email address is not valid. ni***@nigenetDOG.org.uk. Take the dog out! http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
In the land of the blind, the one-eyed man is very, very busy!
LRW <dr***@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 should check this out: http://www.alistapart.com/articles/zebratables/
bblackmoor
2004-03-17 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
2 posts
views
Thread by bettyann |
last post: by
|
2 posts
views
Thread by Ralph Snart |
last post: by
|
47 posts
views
Thread by Matt Kruse |
last post: by
|
9 posts
views
Thread by Max Weebler |
last post: by
|
1 post
views
Thread by Steve Bottoms |
last post: by
|
5 posts
views
Thread by |
last post: by
|
5 posts
views
Thread by eric.goforth |
last post: by
| | | | | | | | | | | |