/    Sign up×
Community /Pin to ProfileBookmark

alternate coloured rows in html table

Part of a script i have that outputs a html table of data from an mysql table is as follows..

[code=php]$query=”SELECT * FROM stats”;
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$username=mysql_result($result,$i,”username”);
$gold=mysql_result($result,$i,”gold”);
$attack=mysql_result($result,$i,”attack”);

Echo ”
<tr>
<td>$username</td>
<td>$gold</td>
<td>$attack</td>
</tr>
“;

$i++;
}[/code]

My problem is – for the html, i would like alternate rows to be in different colours (so they can be read more easily). Such as, first row – blue coloured, second row – light blue, third row – blue, fourth row – light blue. How should i go about doing this?

Thank you for any help ?

Shears

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 23.2006 — Within your output loop, do a modulus by 2 of your loop counter. If the result is 1, set it to one color, else set it to the other color:
[code=php]
$color = ($i % 2 ) ? '#0000CC' : '#3366FF';
Echo "
<tr style='color: $color'>
<td>$username</td>
<td>$gold</td>
<td>$attack</td>
</tr>
";
[/code]
Copy linkTweet thisAlerts:
@NogDogJul 23.2006 — PS: to make the output HTML a bit more lightweight and easier to modify style-wise, instead of explicitly setting a color you could just assign a class to the TR element and specify the appearance in your CSS stylesheet.
Copy linkTweet thisAlerts:
@ShearsauthorJul 23.2006 — Thank you NogDog! That was exactly what i wanted ?

Shears
×

Success!

Help @Shears 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 5.28,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...