Connecting Tech Pros Worldwide Help | Site Map

[SOLVED] Using CSS class to color alternate rows

Newbie
 
Join Date: Oct 2006
Posts: 2
#1: Oct 24 '06
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.
Newbie
 
Join Date: Oct 2006
Posts: 2
#2: Oct 24 '06

re: [SOLVED] Using CSS class to color alternate rows


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.
Closed Thread


Similar PHP bytes