...

View Full Version : Alternating table row background colours



MatesOnline
09-26-2004, 01:54 AM
Hi guys.

ok here is my dilema


<table width="419" height="31" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
<tr bordercolor="#E1E8F2" bgcolor="#E1E8F2" class="smalltxt">
<td width="82" height="13"><strong>Date</strong></td>
<td width="52"><strong>Status</strong></td>
<td width="114"><strong>Sender</strong></td>
<td width="148"><strong>Title</strong></td>
</tr>
<?php do { ?>
<tr class="mediumtxt">
<td height="18"><?php echo $row_listmessages['date']; ?></td>
<td><?php if($row_listmessages['status'] == 'Unread'){
echo "<img src=\images/havemsg.gif alt=Unread>";
} else {
echo "<img src=\images/nomsg.gif alt=Read>";} ?></td>
<td><?php echo $row_listmessages['sender']; ?></td>
<td><?php echo $row_listmessages['title']; ?></td>
</tr>
<?php } while ($row_listmessages = mysql_fetch_assoc($listmessages)); ?>
</table>


as you can see i have a table that produces repeated rows containing data

what i want to do is have the background colour of 1 row say #FF6600
and the following row #FFFFCC for example
and have that repeated down the page

hope i explained that ok ?

hanks in advance guys

dumpfi
09-26-2004, 02:16 AM
<table width="419" height="31" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
<tr bordercolor="#E1E8F2" bgcolor="#E1E8F2" class="smalltxt">
<td width="82" height="13"><strong>Date</strong></td>
<td width="52"><strong>Status</strong></td>
<td width="114"><strong>Sender</strong></td>
<td width="148"><strong>Title</strong></td>
</tr>
<?php
$i = 0;
do { ?>
<tr class="mediumtxt" style="background-color:<?php echo ($i % 2 == 0) ? '#FFFFCC' : '#FF6600'; ?>">
<td height="18"><?php echo $row_listmessages['date']; ?></td>
<td><?php if($row_listmessages['status'] == 'Unread'){
echo "<img src=\images/havemsg.gif alt=Unread>";
} else {
echo "<img src=\images/nomsg.gif alt=Read>";} ?></td>
<td><?php echo $row_listmessages['sender']; ?></td>
<td><?php echo $row_listmessages['title']; ?></td>
</tr>
<?php
$i++;
} while ($row_listmessages = mysql_fetch_assoc($listmessages)); ?>
</table>
dumpfi

MatesOnline
09-26-2004, 02:33 AM
works like a charm

thanks dumfi :)

boeing747fp
09-27-2004, 09:54 PM
i just do it like this



while($row = mysql_fetch_array($query)){
if(empty($color)){
$color = "#FF6600";
}elseif($color == "#FF6600"){
$color = "#FFFFCC";
}elseif($color == "#FFFFCC"){
$color = "#FF6600";
}

//...preceding table code
echo "<tr bgcolor=$color>"
//...proceding table code
}


is that a bad way to do it?

trib4lmaniac
09-27-2004, 10:02 PM
is that a bad way to do it?
I'd say it was a less effecient way to do it. You're having to enter each color twice. The modulus operator is generally the way to go about things like this...


if($i % 2) {
echo '#FFFFFF';
} else {
echo '#E9E9E9';
}

...is (pretty much) the key part of dumpfi's script. It can be extended easily too...


if($i % 3 == 1) {
echo '#FFFFFF';
} else if($i % 3 == 2) {
echo '#E9E9E9';
} else {
echo '#C9C9C9';
}

boeing747fp
09-27-2004, 11:32 PM
o
ok... thanks

jamescover
09-28-2004, 01:07 AM
I just use a simple counter:



$i = 0;

while ($row = mysql_fetch_assoc($makes3)) {

if($i == 1){
echo "<tr bgcolor=\"#FFFFFF\">" . "\n";
$i = 0;
}else{
echo "<tr bgcolor=\"#DFDFDF\">" . "\n";
$i++;
}
echo "The rest of while markup here";
}


-james

Celtboy
09-28-2004, 01:25 AM
I'd switch to a css solution.

class="even_row" & class="odd_row"

Kurashu
09-28-2004, 01:29 AM
I know I'm gonna get smacked for this, but I use a tenary with two CSS classes in them for mine.

*example from my tagboard*

<?php
$x = 0;
while($row = pg_fetch_array($sql,NULL,PGSQL_ASSOC))
{
$class = ($x % 2) ? ("content1") : ("content2");
$row["comment"] = eregi_replace($patterns, $replacements, $row["comment"]);
$row["name"] = eregi_replace($patterns, $replacements, $row["name"]);
print "<tr><td class=\"$class\"><strong>". $row["name"] ."</strong>\n". $row["comment"] ."\n";
if ($row["www"]) { print " <strong><a class=\"archive\" href=\"". $row["www"] ."\">www</a></strong>";}
print "</td></tr>\n";
$x++;
}
empty($x);
?>

I've gotten into the habit of emptying my variables I use for counting. That's why there is a an empty() floating in there.

trib4lmaniac
09-28-2004, 09:58 PM
I'd switch to a css solution.

class="even_row" & class="odd_row"
How does that help? I can't seea completely css way of doing this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum