473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

[SOLVED] Using CSS class to color alternate rows

I am trying to use a CSS class called "evens" to color the background of alternate rows of a dynamic table. The foreach statement is preventing the data from being displayed at all. If I take it out, I have no problem generating the table. What is the proper way to do this?

=====================
// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5" border="0">
<tr>
<td align="left"><strong>Edit</strong></td>
<td align="left"><strong>Delete</strong></td>
<td align="left"><strong>Award Title</strong></td>
<td align="left"><strong>Award Category</strong></td>
<td align="left"><strong>Award Text</strong></td></tr>';

// Fetch and print all records.
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

$n = 0;
foreach($result as $row) {
$theclass = ($n++ % 2) ? 'evens' : '';

echo '<tr class="' . $theclass .'">';
echo '<td align="left"><a href="edit_awards.php?id=' . $row['awardID'] . '">Edit</a></td>';
echo '<td align="left"><a href="delete_awards.php?id=' . $row['awardID'] . '">Delete</a></td>';
echo '<td align="left"><span class="award_title">' . $row['awardTitle'] . '</span></td>';
echo '<td align="left">' . $row['awardCat'] . '</td>';
echo '<td align="left">' . $row['awardText'] . '</td>';
echo '</tr>';
}

}
=====================

Any ideas? TIA.
Oct 24 '06 #1
1 2446
Nevermind. I found out a useful tip at the Practical Web Design mag site, and deduced the rest for myself.

Fixed code:
===================
// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5" border="0">
<tr>
<td align="left"><strong>Edit</strong></td>
<td align="left"><strong>Delete</strong></td>
<td align="left"><strong>Award Title</strong></td>
<td align="left"><strong>Award Category</strong></td>
<td align="left"><strong>Award Text</strong></td></tr>';

// Fetch and print all records.
$n = 0;
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

$theclass = ($n++ % 2) ? 'evens' : '';

echo '<tr class="' . $theclass .'">';
echo '<td align="left"><a href="edit_awards.php?id=' . $row['awardID'] . '">Edit</a></td>';
echo '<td align="left"><a href="delete_awards.php?id=' . $row['awardID'] . '">Delete</a></td>';
echo '<td align="left"><span class="award_title">' . $row['awardTitle'] . '</span></td>';
echo '<td align="left">' . $row['awardCat'] . '</td>';
echo '<td align="left">' . $row['awardText'] . '</td>';
echo '</tr>';

}
========================

It now works like a charm. Thanks anyway.
Oct 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ralph Snart | last post by:
Is there a way to alternate table row colors without using the position() mod 2 trick? I'm in a series of nested xsl:for-each elements, about 3 deep, and I want to alternate the table row color...
1
by: F. Da Costa | last post by:
Hi, In the html (as shown below) there are some <a> tags I need to move into a map. This is no problem (in Gecko) using the js below. However, IE 5+ (nor 6) does not want to play. It gives me...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
4
by: sconeek | last post by:
Hi all, I am generating a html based table with multiple rows of data coming in real time from a postgres DB. The underlying technology is java based however the front end is html. now i am...
3
by: bob.herbst | last post by:
I have been trying to use HTML_Table from PEAR to write a PHP script that will access a database and retrieve my data into an HTML table that can be sorted by column. Currently I am using the...
5
by: eric dexter | last post by:
I am just trying to acess a function in wordgrid (savefile) to a button that is defined in TestFrame. I can't seem to make it work I either get an error that my variable isn't global or it makes...
43
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
0
by: vilas patil | last post by:
How I Can Add Alternate Color To The Rows Of Dbgrid
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.