Forum Moderators: coopster

Message Too Old, No Replies

Making php to change the color of the background color of a table row

to change the color of the next retrieved html table row from a database

         

dbarasuk

1:52 am on Dec 30, 2007 (gmt 0)

10+ Year Member



Hi,
I have a script retrieving the content of a table designed to hold articles and I would like to change the background color of the next retrieved row.

I have a problem because the background-color is always the same while I expect it to change for the next retrieved row.

If you use phpMyAdmin(which you do)you have seen that row color is repeated for every next row in an alternative way.

Can you spot the error i made that keeps me from getting the intended result?

Here's the code:

<?php
$i = 0;
while($row = mysql_fetch_assoc($result)) {?>

<?php
$i++;

if($i%2)
{
$bg_color = "#EEEEEE";
}
else {
$bg_color = "#E0E0E0";
}
?>

<tr bgcolor="$bg_color">

<td width="10%" style="border:0;"><?php echo $row["name"];?></td>
<td width="10%" style="text-align:right; border:0;"><?php echo $row["title"];?></td>
<td width="60%" style="text-align:right; padding-left:5;"><?php
$extract = substr($row["content"], 0, 100);
$lastSpace = strrpos($extract, " ");
if(strlen($row["content"]) <= 101) {

echo $row["content"];
}
else {
echo substr($row['content'], 0, $lastSpace)."... <a href='article.php?article_id=". $row['article_id']."'>
Read More </a>";
}
?>
</td>
<td width="10%" style="border:0; text-align:right;"><a href="update_article.php?article_id=<?php echo $row["article_id"];?>">EDIT</a> </td>
<td width="10%" style="border:0; text-align:right;"><a href="delete_article.php?article_id=<?php echo $row["article_id"];?>">DELETE</a></td>
</tr> <?php }

?>

Regards,
dbarasuk

Habtom

7:25 am on Dec 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line: <tr bgcolor="$bg_color">

needs to be changed to:

<?php echo "<tr bgcolor='". $bg_color ."'>";?>

Matsuibiza

7:30 am on Dec 30, 2007 (gmt 0)

10+ Year Member



Change this line:

<tr bgcolor="$bg_color">

To:

<tr style="background-color: <?php echo $bg_color;?>">

(If I remember right, bgcolor is not part of W3C)

dbarasuk

11:43 pm on Jan 1, 2008 (gmt 0)

10+ Year Member



thanks,
it works perfectly now.
regards,
dbarasuk