Solved

PHP MYSQL CSS: How do you have different background shade for alternate rows?

Posted on 2008-06-14
8
638 Views
Last Modified: 2013-12-13
PHP MYSQL CSS


Hello,

How do I output data from a MySQL table in such a way the background of alternate rows are different?

For example...

[td bgcolor=grey] ROW 1 [/td]
[td bgcolor=white] ROW 2 [/td]
[td bgcolor=grey] ROW 3 [/td]
[td bgcolor=white] ROW 4 [/td]
[td bgcolor=grey] ROW 5 [/td]

0
Comment
Question by:gingera
  • 4
  • 3
8 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
ID: 21786431
Refer to the following
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
	<style type="text/css"><!--
	tr, td{width:100% !important;}
	.odd{background-color:lightblue;}
	.even{background-color:khaki;}
	--></style> 
<script type="text/javascript"><!--
window.onload=init
function init()
{
	var tr = document.getElementsByTagName("tr");
	for( var i=0; i < tr.length; ++i)
	{
		tr[i].className = (i % 2) ? "even" : "odd";
	}
}
//--></script>
</head>
<body>
<table width="100%"> 
	<tr><td>a</td></tr>
	<tr><td>b</td></tr>
	<tr><td>c</td></tr>
	<tr><td>d</td></tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 2

Accepted Solution

by:
jml948 earned 200 total points
ID: 21786442
try this:
$i = 0;
while(($row = mysql_fetch_row($result)) !== false) {
	$i++;
	print "<td bgcolor=\"". (($i % 2 == 0)? $color1:$color2) ."\">;
	print $row[0];
	print "</td>\n";
}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 21786461
If you want to do this on the server, see attached code:
NOTE: <tr> is the row tag, not <td>

$i = 0;
while(($row = mysql_fetch_row($result)) !== false) {
      $i++;
      print "<tr bgcolor='". (($i % 2 == 0)? $color1:$color2) . "'>";
      print "<td>{$row[0]}</td>";
      print "</tr>\n";
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
ID: 21786464
In case it's not obvious, you need to initialize the color variables as well:
$i = 0;
$color1="lightblue";
$color2="khaki";
while(($row = mysql_fetch_row($result)) !== false) {
      $i++;
      print "<tr bgcolor='". (($i % 2 == 0)? $color1:$color2) . "'>";
      print "<td>{$row[0]}</td>";
      print "</tr>\n";
}

Open in new window

0
 

Author Comment

by:gingera
ID: 21786542
Wow... thanks! I will try it out!
0
 

Author Comment

by:gingera
ID: 21786556
> In case it's not obvious

Nothing is obvious to this stupid newbie (me)! :-)

THANKS for clarifying, Hielo! I love it when people talk to me at the most basic level, 'cos I really am not an expert. :-)
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
ID: 21787395
>>THANKS for clarifying,
No problem. If your record contains more than one field, you can print them within the same <tr> - ex, assuming your records contain three fields:
$i = 0;
$color1="lightblue";
$color2="khaki";
while(($row = mysql_fetch_row($result)) !== false) {
      $i++;
      print "<tr bgcolor='". (($i % 2 == 0)? $color1:$color2) . "'>";
      print "<td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td>";
      print "</tr>\n";
}

Open in new window

0
 

Author Closing Comment

by:gingera
ID: 31467233
Your help is very much appreciated by this newbie!!! ;-) THANKS!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question