Jump to content

how do i set alternating row background colors?


jbrill

Recommended Posts

Im trying to create alternation rows for a table.. heres my code:

<?
echo '<table border="0" bgcolor="#ffffff" class="MainBody1" width="50%" align="center">';
$result = mysql_query("SELECT * FROM workhours WHERE jobid='".$jobid."' AND stepid='".$idr."'");
while($row = mysql_fetch_array($result))
{

echo '<tr>';
echo '<td width="80" class="underline">'. $row['date'] .'</td>';
echo '<td width="120" class="underline">'. $row['name'] .'</td>';
echo '<td width="80" class="underline">'. $row['hours'] .'</td>';
echo '<td class="underline">'. $row['notes'] .'</td>';
echo '</tr>';
}
echo '</table><br>';
?>

 

how would i alternate background colors so that one is white and the next is #cccccc and then white and so on...

 

 

can u put that into my code in the first post? i really have no clue... new to php

 

Try it yourself. If it works, you'll have learned something. If it doesn't you'll have learned something and can come back with your modified code for more help.  We help you, not code for you.

anyone?

 

 

Hey,

 

Here is your code

<?
echo '<table border="0" bgcolor="#ffffff" class="MainBody1" width="50%" align="center">';
$result = mysql_query("SELECT * FROM workhours WHERE jobid='".$jobid."' AND stepid='".$idr."'");
while($row = mysql_fetch_array($result))
{
$css_class = ($css_class=='row_style_1') ? 'row_style_2' : 'row_style_1';
echo "<tr class='$css_class'>";
echo '<td width="80" class="underline">'. $row['date'] .'</td>';
echo '<td width="120" class="underline">'. $row['name'] .'</td>';
echo '<td width="80" class="underline">'. $row['hours'] .'</td>';
echo '<td class="underline">'. $row['notes'] .'</td>';
echo '</tr>';
}
echo '</table><br>';
?>

 

AND HERE is the CSS CODE FOR IT

<style type='text/css'>
.row_stlye_1
{
    background-color: #eaeaea;
}
.row_stlye_2
{
    background-color: #ffffff;
}

</style>

 

That should be it.

 

Thanks

 

Jyot

Try this:

<style type='text/css'>
.row_style_1, .row_style_1 td
{
    background-color: #eaeaea;
}
.row_style_2, .row_style_2 td
{
    background-color: #ffffff;
}
</style>

 

Otherwise it's just applying the style to the row itself, and not to the individual table cells.

Try removing bgcolor="#ffffff" from the table tag and see if that makes any difference.

 

Also, try changing the color values for row_style_1 and/or row_style_2 to see if that changes the output - it may at least help us see if it's actually changing the background color.

the source code looks like the tables are getting the classes, but the styles arent actually stylizing

 

Could you post the CSS you are actually using, as well as post only enough of the generated HTML code to show us two complete rows of your table.

ok, here is the php code using the css:


<?
echo '<table border="0" bgcolor="#ffffff" class="MainBody1" width="50%" align="center" cellpading="0" cellspacing="0">';
echo '<tr><td class="underline">Date</td><td class="underline">Name</td><td class="underline">Hours</td><td class="underline">Notes</td>';
$result = mysql_query("SELECT * FROM workhours WHERE jobid='".$jobid."' AND stepid='".$idr."'");
while($row = mysql_fetch_array($result))
{

$css_class = ($css_class=='row_style_1') ? 'row_style_2' : 'row_style_1';
echo "<tr class='$css_class'>";
echo '<td width="80">'. $row['date'] .'</td>';
echo '<td width="120">'. $row['name'] .'</td>';
echo '<td width="80">'. $row['hours'] .'</td>';
echo '<td>'. $row['timenotes'] .'</td>';
echo '</tr>';
}
echo '</table><br>';
?>

 

Here is the css code in my attached style sheet:

.row_style_1, td.row_style_1
{
    background-color: #eaeaea;
}
.row_style_2, td.row_style_2
{
    background-color: #ffffff;

 

and here is the source code it produces:

<table border="0" bgcolor="#ffffff" class="MainBody1" width="50%" align="center" cellpading="0" cellspacing="0"><tr><td class="underline">Date</td><td class="underline">Name</td><td class="underline">Hours</td><td class="underline">Notes</td><tr class='row_style_1'><td width="80">12-07-2007</td><td width="120">Jordan </td><td width="80">12</td><td>be careful needs new blade</td></tr><tr class='row_style_2'><td width="80">12-07-2007</td><td width="120">Jordan </td><td width="80">10</td><td>test</td></tr><tr class='row_style_1'><td width="80">12-07-2007</td><td width="120">Jordan </td><td width="80">5</td><td></td></tr></table><br>

 

 

 

 

I took your generated html and style and combined them. It produces alternate colour rows .... once you add the closing curly brace to the CSS

 

.row_style_1, td.row_style_1
{
    background-color: #eaeaea;
}
.row_style_2, td.row_style_2
{
    background-color: #ffffff;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.