Jump to content

Recommended Posts

This is probably completely wrong but I am trying to figure out how to alternate table row colors.  The color I am getting is a bright green for all the rows, even though I clearly specify RED and GRAY.

 

<table>
<tr>
	<th><div align="left">Program</div></th>
	<th><div align="left">Session</div></th>
	<th><div align="left">Start Date</div></th>
	<th><div align="left">End Date</div></th>
</tr>
<?php do {

$color1 = "#FF0000";  
$color2 = "#808080";  
$count = 0;
$color = ($count % 2) ? $color1 : $color2; 

?>
<tr>
	<td bgcolor="$row_color"><?php echo $row_programs['pname']; ?></td>
	<td bgcolor="$row_color"><?php echo $row_programs['sname']; ?></td>
	<td bgcolor="$row_color"><?php echo $row_programs['start_date']; ?></td>
	<td bgcolor="$row_color"><?php echo $row_programs['end_date']; ?></td>
</tr>
<?php $count++; } while ($row_programs = mysql_fetch_assoc($programs)); ?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/242403-problem-alternating-table-row-colors/
Share on other sites

You are defining $count inside the loop as 0 on each iteration! But, you don't need it anyway. There are other problems in that you are trying to put PHP variables directly in the HTML. Also, the do while loop will fail to have values the first time through.

 

<?php

$color1 = "#FF0000";  
$color2 = "#808080";  

$output = '';
while ($row_programs = mysql_fetch_assoc($programs))
{
    $row_color = ($row_color==$color1) ? $color2 : $color1; 
    $output .= "<tr>\n";
    $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['pname']}</td>\n";
    $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['sname']}</td>\n";
    $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['start_date']}</td>\n";
    $output .= "<td bgcolor=\"{$row_color}\">{$row_programs['end_date']}</td>\n";
    $output .= "</tr>\n";
}
?>
<table>
    <tr>
        <th><div align="left">Program</div></th>
        <th><div align="left">Session</div></th>
        <th><div align="left">Start Date</div></th>
        <th><div align="left">End Date</div></th>
    </tr>
    <?php echo $output; ?>
</table>

The above code works, but it also outputs an error that $row_color is an undefined variable.

 

Yeah, that is a warning because the first time you check "($row_color==$color1)" it is not yet defined. But, you shouldn't be displaying warning errors in your production environment anyway. So, you can either ignore it or just define $row_color as $color1 before the loop starts

$color1 = "#FF0000";  
$color2 = "#808080";  
$row_color = $color1; // <=== NEW LINE

$output = '';
while ($row_programs = mysql_fetch_assoc($programs))
{

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.