/    Sign up×
Community /Pin to ProfileBookmark

while loop that alternates row colors

I need to create a while loop that builds a table with alternating colors. I have the css setup but am not sure how write the loop. I’m building the table with data from a db query.

The table rows will look something like this when done:

[CODE]
<tr class=”even”>
<td width=”102″><img src=”assets/images/recSold/lionel.jpg” alt=”” /></td>
<td width=”212″><h2>Item Title</h2>
<p> Item number: 000000</p></td>
<td width=”73″><div align=”center”>1</div></td>
<td width=”61″><div align=”center”>Yes</div></td>
<td width=”92″><a href=”#”>EDIT THIS ITEM</a></td>
</tr>
<tr class=”odd”>
<td width=”102″ ><img src=”assets/images/recSold/tennis.jpg” alt=”” /></td>
<td width=”212″ ><h2>Item Title</h2>
<p> Item number: 000000</p></td>
<td width=”73″><div align=”center”>2</div></td>
<td width=”61″><div align=”center”>Yes</div></td>
<td width=”92″ ><a href=”#”>EDIT THIS ITEM</a></td>
</tr>
[/CODE]

The PHP I have started is here:

[code=php]<?php $item_set = get_auction_items($public = false); ?>
<?php
while ($item = mysql_fetch_array($item_set)) {
// yeah I didn’t get too far
// is it possible to just check for even or odd rows somehow?
}
?>
[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@po3authorDec 24.2007 — I think I might have it.

[code=php]
$count = 1;
while ($item = mysql_fetch_array($item_set)) {
if ($count % 2) {
echo "even - ";
} else {
echo "odd - ";
}
$count ++;
}
[/code]


Should have done a search first : ) I'm gonna give this a try.
×

Success!

Help @po3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...