Solved

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

Posted on 2008-06-14
8
643 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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 demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

691 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