473,586 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

alternating row colors on query generated table?

LRW
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($q uery_RSwho, $connection) or die("Couldn't query: "
.. mysql_error());
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$names .= "<tr align=left><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who ['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
Jul 17 '05 #1
12 2868
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($q uery_RSwho, $connection) or die("Couldn't query: "
. mysql_error());
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$names .= "<tr align=left><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who ['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_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$bgcolour = ($i%2) ? 'bgcolor=\'whit e\'' : 'bgcolor=\'grey \'';
++$i;
$names .= "<tr align=left " . $bgcolour . "><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who['email']."</td></tr>";
}
}

--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Jul 17 '05 #2
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($q uery_RSwho, $connection) or die("Couldn't query: "
. mysql_error());
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$names .= "<tr align=left><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who ['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 =--
Jul 17 '05 #3
LRW
"Mike Peters" <o0************ ****@THIShotmai l.com> wrote in message
news:b3******** *************** *******@news.te ranews.com...
I'd do it something like this:

$i = 1;
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$bgcolour = ($i%2) ? 'bgcolor=\'whit e\'' : 'bgcolor=\'grey \'';
++$i;
$names .= "<tr align=left " . $bgcolour . "><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who['email']."</td></tr>";
}
}


Excellent! Perfect! Works prefectly. =)
Thanks you! (Oh...I can do so much with this....)
Thanks! =)
Liam
Jul 17 '05 #4
On 2004-03-07, LRW <dr***@NOSPAHMc elticbear.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($q uery_RSwho, $connection) or die("Couldn't query: "
. mysql_error());
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$names .= "<tr align=left><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who ['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
Jul 17 '05 #5
In message <rMM2c.79807$ko 6.426465@attbi_ s02>, LRW
<dr***@NOSPAHMc elticbear.com> writes
"Mike Peters" <o0************ ****@THIShotmai l.com> wrote in message
news:b3******* *************** ********@news.t eranews.com...
I'd do it something like this:

$i = 1;
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$bgcolour = ($i%2) ? 'bgcolor=\'whit e\'' : 'bgcolor=\'grey \'';
++$i;
$names .= "<tr align=left " . $bgcolour . "><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who['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
Jul 17 '05 #6
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($q uery_RSwho, $connection) or die("Couldn't query: "
. mysql_error());
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$names .= "<tr align=left><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who ['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_arr ay($result)) {
$bgcolor = ($bgcolor == $bgcolor1) ? $bgcolor2: $bgcolor1;
echo '<tr><td bgcolor="'.$bgc olor.'">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***@glassgian t.com
http://www.glassgiant.com
Jul 17 '05 #7
LRW
Five Cats wrote:
In message <rMM2c.79807$ko 6.426465@attbi_ s02>, LRW
<dr***@NOSPAHMc elticbear.com> writes
"Mike Peters" <o0************ ****@THIShotmai l.com> wrote in message
news:b3****** *************** *********@news. teranews.com...
I'd do it something like this:

$i = 1;
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$bgcolour = ($i%2) ? 'bgcolor=\'whit e\'' :
'bgcolor=\'grey \'';
++$i;
$names .= "<tr align=left " . $bgcolour . "><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who['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_co lor\' use the class tag in that where/if?

Liam
Jul 17 '05 #8
LRW <dr***@NOSPAHMc elticbear.com> wrote in
news:z893c.5063 50$I06.5412308@ attbi_s01:
Five Cats wrote:
$i = 1;
while ($row_RSwho = mysql_fetch_arr ay($RSwho)) {
if ($row_RSwho['name'] != "X") {
$bgcolour = ($i%2) ? 'bgcolor=\'whit e\'' :
'bgcolor=\'grey \'';
++$i;
$names .= "<tr align=left " . $bgcolour . "><td
width=120>".$ro w_RSwho['name']."</td><td
align=left>&nbs p;--&nbsp;".$row_RS who['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_co lor\' use the class tag in that where/if?


Class *attribute*. Yep, change "bgcolor='white '" to "class='odd '" and
"bgcolor='grey' " to "class='eve n'". 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.
Jul 17 '05 #9
"LRW" <dr***@NOSPAHMc elticbear.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/
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1739
by: bettyann | last post by:
i am using templates via HTML_Template_IT to create a table from the results of a mysql query. i wanted to change the background image of alternating rows in the table. i thought i could do this by defining two different blocks in the template file and setting the current block based on ( $thisRowNum % 2 ). when the final table is...
2
6179
by: Ralph Snart | last post by:
Is there a way to alternate table row colors without using the position() mod 2 trick? I'm in a series of nested xsl:for-each elements, about 3 deep, and I want to alternate the table row color all the way through. position() resets to 1 whenever the inner for-each loops around, of course. Example code: <xsl:for-each...
47
18870
by: Matt Kruse | last post by:
http://www.mattkruse.com/temp/css_expressions.html One of the standard CSS questions is "how can I shade every other table row a different color with CSS?" The answers are usually 1) you can't (yet), 2) put a class on every other tr, or 3) use javascript to do the styling for you. Well, I've been playing the CSS expressions quite a bit...
9
2685
by: Max Weebler | last post by:
Hi, I have a datagrid built that has an alternating item style that sets the backcolor and ForeColor of its rows. I have 4 template columns. One of them has a LinkButton embedded in it to perform updates. All the styles that are set are being followed by all templated columns with the exception for the update column. The update column...
1
1307
by: Steve Bottoms | last post by:
Hi, all! How does one prevent table cell background colors from alternating between grey and white? I've dropped a basic table control on an ASP.Net page (using VB.Net codebehind), and dynamically creating rows and cells. However, I can't seem to get the blasted thing to stop alternating the background colors on every other row! I've...
5
3334
by: | last post by:
I'm making admin forms. I'm wondering if there is a way to have the server programmatically assign alternating colors in a regular table (not a datalist control). I notice ASP.NET 2 offers a runat="server" attribute on tables, leading me to believe that this should be possible. Any ideas? I'm using C#. Thanks, -KF
5
1465
by: eric.goforth | last post by:
Hello, I have some xml data that look like: <currencysummary rec_count="16"> <currency> <currency_description>$</_currency_description> <currency_code>1</_currency_code> </currency> <currency>
3
1561
clintw
by: clintw | last post by:
Hi all, I've been doing my head in trying to get this to work, but I keep running into a snag. Basically I have a database table with a list of events for the year, with fields for Month, Event Description, FullDate (used for sorting). When I input the result set into a HTML table, this works fine, alternating backgrounds colors for the...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.