Solved

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

Posted on 2008-06-14
8
626 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 82

Assisted Solution

by:hielo
hielo earned 300 total points
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:gingera
Comment Utility
Wow... thanks! I will try it out!
0
 

Author Comment

by:gingera
Comment Utility
> 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
Comment Utility
>>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
Comment Utility
Your help is very much appreciated by this newbie!!! ;-) THANKS!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now