Solved

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

Posted on 2008-06-14
8
640 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bootstrap form validations! 1 40
Load string Array from file 23 41
Position image fpdf 4 17
Last 4 numbers of a variable 9 20
This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

732 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