473,776 Members | 1,503 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 2894
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
1757
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 displayed, all the "even" rows are displayed before all the "odd" rows. in other words, the rows are not...
2
6195
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 select="/games/game) = 1]"> <xsl:sort select="platform"/>
47
18905
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 lately and I've found a much simpler solution which works in IE5.5+, using "expressions". If
9
2695
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 does set the BackColor appropriately but when setting its ForeColor the LinkButton's color is blue and...
1
1316
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 tried setting the cell.BackColor property, but the value is just getting ignored! Also tried on...
5
3346
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
1476
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
1568
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 individual rows is no problem - my problem comes in trying to alternate the background colors by Month...
0
9628
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10289
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5367
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.