/    Sign up×
Community /Pin to ProfileBookmark

Alternate Table Row Color for My_Sql Data

How can I make every other row a different color, while pulling data from a database. The code is below:

[code=php]

mysql_select_db(“email_request”, $con);$result = mysql_query(“SELECT * FROM Email ORDER BY Date DESC”);

echo “<table class=’sortable’>
<tr>
<th>Name</th>
<th>Email</th>
<th>Date</th>
</tr>”;

while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘Name’] . “</td>”;
echo “<td>” . $row[‘Email’] . “</td>”;
echo “<td>” . $row[‘Date’] . “</td>”;
echo “</tr>”;
}
echo “</table>”;

[/code]

I have been allover different forums trying to figure out an easy way to do this. Any help would be much appreciated.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 12.2008 — [code=php]$red = 1;
while($row = mysql_fetch_array($result))
{
echo "<tr class='" . ($red?'red':'blue') . "'>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
$red = !$red;
}
echo "</table>";[/code]

and some pretty CSS:
<i>
</i>tr.red, tr.red td{
background-color: red;
}
tr.blue, tr.blue td{
background-color: blue;
}

play around with it.
Copy linkTweet thisAlerts:
@1234567890authorNov 12.2008 — I put the above in with no luck ? It changed nothing on my end....
Copy linkTweet thisAlerts:
@TaboNov 12.2008 — [code=php]<?php
// ...

while($row = mysql_fetch_array($result)){
// ...
echo "<tr class="".($row % 2)?"red":"blue"."">
//...
}
?>[/code]


haven't tested and not good w/ php but I think that should work, if not it is because of the ternary ([I]condition?if true:if false[/I])
Copy linkTweet thisAlerts:
@1234567890authorNov 12.2008 — The following code does nothing - but doesn't produce any errors. I have to be missing something on this!

[code=php]

while($row = mysql_fetch_array($result))
{
echo "<tr class="".($row % 2)?"red":"blue"."">";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";[/code]
Copy linkTweet thisAlerts:
@scragarNov 12.2008 — [code=php]<?php
// ...

while($row = mysql_fetch_array($result)){
// ...
echo "<tr class="".($row % 2)?"red":"blue"."">
//...
}
?>[/code]


haven't tested and not good w/ php but I think that should work, if not it is because of the ternary ([I]condition?if true:if false[/I])[/QUOTE]

see, now your's is exactly the same as mine in theory, but your's won't work(row is an array, not a number = error), where mine does. Oh, and the last part:[code=php]...?"red":"blue"."">[/code]Works out the same as:[code=php]
...?"red":"blue">[/code]
so yeah, that's a failure(wrap brackets around the whole ternary thing to cancel that little error out).

Anyway, I'm going to guess that the code is working perfectly, but the OP has just done something wrong, perhaps not having understood the use of CSS, or maybe a silly mistake adding a more global CSS condition after it, perhaps (s)he changed a class name but forgot to account for it, or maybe the page is just being cached and it just needs a good shift+F5 combo to force reloading, who knows till the OP provides some details or a link.
Copy linkTweet thisAlerts:
@1234567890authorNov 12.2008 — the page is password protected so providing a link would make it hard.

all i get with the above code is "red" repeated a thousand times before my table. i also have no css attributed to this table.

if anyother information is needed i will provide!
Copy linkTweet thisAlerts:
@scragarNov 12.2008 — ?

What code exactly are you using? The code should assign a class, you don't see classes as text unless you've done something wrong.
Copy linkTweet thisAlerts:
@1234567890authorNov 12.2008 — I am really dumb. The first response worked all along, and I am just stupid php n00b. Many thanks!
Copy linkTweet thisAlerts:
@TaboNov 12.2008 — see, now your's is exactly the same as mine in theory, but your's won't work(row is an array, not a number = error), where mine does. Oh, and the last part:[code=php]...?"red":"blue"."">[/code]Works out the same as:[code=php]
...?"red":"blue">[/code]
so yeah, that's a failure(wrap brackets around the whole ternary thing to cancel that little error out).

Anyway, I'm going to guess that the code is working perfectly, but the OP has just done something wrong, perhaps not having understood the use of CSS, or maybe a silly mistake adding a more global CSS condition after it, perhaps (s)he changed a class name but forgot to account for it, or maybe the page is just being cached and it just needs a good shift+F5 combo to force reloading, who knows till the OP provides some details or a link.[/QUOTE]

Oops, I'm used to js loops like for(var i=0;i<x.length...

what way could I do my example? So say that $number = the number of the current item in the array??
Copy linkTweet thisAlerts:
@scragarNov 12.2008 — Oops, I'm used to js loops like for(var i=0;i<x.length...

what way could I do my example? So say that $number = the number of the current item in the array??[/QUOTE]


You would need to institute a counter, this can be done in a for()} loop with a few minior changes:
[code=php]
//while($row = mysql_fetch_array($result))
for($count = 0, $row = mysql_fetch_array($result); $row = mysql_fetch_array($result); $count++)
{
// echo "<tr class="".($row &#37; 2)?"red":"blue"."">";
echo '<tr class="'.(($count % 2)?"red":"blue").'">';

echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
[/code]
Oh, and for future reference, to test if a number is odd or even:
[code=php]($count & 1)[/code]is much faster.
Copy linkTweet thisAlerts:
@TaboNov 12.2008 — You would need to institute a counter, this can be done in a for()} loop with a few minior changes:
[code=php]
//while($row = mysql_fetch_array($result))
for($count = 0, $row = mysql_fetch_array($result); $row = mysql_fetch_array($result); $count++)
{
// echo "<tr class="".($row % 2)?"red":"blue"."">";
echo '<tr class="'.(($count % 2)?"red":"blue").'">';

echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
[/code]
Oh, and for future reference, to test if a number is odd or even:
[code=php]($count & 1)[/code]is much faster.[/QUOTE]


Thanks, is the while loop much faster though?
Copy linkTweet thisAlerts:
@scragarNov 12.2008 — well this for loop is built atop the while loop, so your looking at an extra calculation each iteration, 1 extra variable, and a bit slower on initiation, so it's not much slower(it your looping a few million times it might be noticed, but then the efficiency isn't your problem ?), but it is slower.
×

Success!

Help @1234567890 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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