Solved

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

Posted on 2008-06-14
8
635 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

821 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