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

 

 

Link to comment
Share on other sites

I do a for loop that spits out the html for the row then inside the for loop i have an if statment. $

 

for($i=0;$i<=$numberofrows;$i++){

HTMLCODEFORROWHERE

if($i%2==0){

echo 'codeforonebgcolor';

}else{

echo 'codeforotherbgcolor';

}

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

 

 

 

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.