Page 1 of 1
Alternating table row colours
Posted: Thu Mar 23, 2006 11:26 am
by mjmacarty
Pimptastic | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I am trying to alternate the background color of a table generated from PHP. I thought something like the following would work provided I pass $table_color to the <tr>
Code: Select all
if (($post_id % 2) == 1) {
$table_color = "A1A1A1";
} else {
$table_color = "C9C9C9";
}
I was wrong though. What gives?
Pimptastic | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Thu Mar 23, 2006 11:28 am
by JayBird
Please use more informative topic titles
Thanks
Posted: Thu Mar 23, 2006 11:32 am
by feyd
it'd help to understand how you were using $table_color by post a bit more code.
Here is some more code
Posted: Thu Mar 23, 2006 12:59 pm
by mjmacarty
feyd | Please use Code: Select all
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Here is moe context:
Code: Select all
//while ($posts_info = mysql_fetch_array($get_posts_res)) {
$post_id = $posts_info['post_id'];
$post_text = nl2br(stripslashes($posts_info['post_text']));
$post_create_time = $posts_info['fmt_post_create_time'];
$post_owner = stripslashes($posts_info['post_owner']);
// add to display
if (($post_id % 2) == 1) {
$table_color = "A1A1A1";
} else {
$table_color = "C9C9C9";
}
$display_block .="
<tr style=\"background-color:#$table_color\">
<td width=25% valign=top>$post_owner<br>[$post_create_time]</td>
<td width=75% valign=top>$post_text<br><br>
<a href=\"replytopost.php?post_id=$post_id\"><b>REPLY TO POST</a></td>
</tr>";
}
// close table
$display_block .= "</table>";
}
feyd | Please use Code: Select all
tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Mar 23, 2006 2:10 pm
by mattcooper
You're using modulus to alternate the table rows, which is fine but not the way that I do it. Here is a sample bit of code from one of my recent developments that will help you:
Code: Select all
$num=mysql_num_rows($result);
$i=0;
$number=1;
$alt1="#FFB3B3";
$alt2="#FFFFFF";
$alternate=$alt1;
//Pages to exclude DEFINITELY from the list of available pages, since they are purely dynamic and have no editable content
$ignore=array("confirm","contact_confirm","error","faq_confirm","logout","unsubscribe","unsubscribe_confirm","profile");
while($row = mysql_fetch_array($result)) {
if(!in_array($row["pageid"], $ignore)) {
//Generate the table rows and populate them dynamically
$i++;
if($row['hidden']=="0"){
echo "<tr bgcolor=$alternate><td width=\"250\">$row[pageid]</td><td> </td><td>$row[lastdate]</td><td> </td><td><a href=\"pageadmin.php?pagename=$row[pageid]\"><img src=\"edit.gif\" alt=\"Edit\" border=\"0\"></a> </td><td><a href=\"../home.php?pagename=$row[pageid]\" target=\"_blank\"><img src=\"view.gif\" alt=\"View\" border=\"0\"></a> </td><td><a href=\"{$PHP_SELF}?pagename=$row[pageid]&do=trash\"><img src=\"trash.gif\" alt=\"Trash\" border=\"0\"></a> </td><td><a href=\"{$PHP_SELF}?pagename=$row[pageid]&do=hide\"><img src=\"delete.gif\" alt=\"Hide\" border=\"0\"></a></td><td> <b>$row[hits] hits</b></tr>";
$number = $number + 1;
if($alternate==$alt1){
$alternate=$alt2;
}
else {
$alternate=$alt1;
}
}
}
}
Pluck out the bits you need and me know how you go!
Posted: Thu Mar 23, 2006 2:44 pm
by mjmacarty
Okay Let me try this one more time
Code: Select all
while ($posts_info = mysql_fetch_array($get_posts_res)) {
$post_id = $posts_info['post_id'];
$post_text = nl2br(stripslashes($posts_info['post_text']));
$post_create_time = $posts_info['fmt_post_create_time'];
$post_owner = stripslashes($posts_info['post_owner']);
// add to display
if (($post_id % 2) == 1) {
$table_color = "A1A1A1";
} else {
$table_color = "C9C9C9";
}
$display_block .="
<tr style=\"background-color:#$table_color\">
<td width=25% valign=top>$post_owner<br>[$post_create_time]</td>
<td width=75% valign=top>$post_text<br><br>
<a href=\"replytopost.php?post_id=$post_id\"><b>REPLY TO POST</a></td>
</tr>";
}
// close table
$display_block .= "</table>";
}
Posted: Thu Mar 23, 2006 3:37 pm
by pickle
This has been covered lots before on this board. Doing a search will certainly get you what you need.
My particular method would be:
Code: Select all
while ($posts_info = mysql_fetch_array($get_posts_res)) {
//your other stuff
$table_color = ($table_color == 'A1A1A1') ? 'C9C9C9' : 'A1A1A1';
$display_block .= <<<USEHEREDOCS
<tr style = "background-color:#$table_color">
USEHEREDOCS;
//rest of your stuff
}
Also, heredocs are way easier to use than double quotes, when outputing complex strings like that.
Posted: Thu Mar 23, 2006 8:18 pm
by mjmacarty
Thanks very much pickle. I am just not sure why the code I wrote wouldn't work?
Posted: Thu Mar 23, 2006 8:24 pm
by Benjamin
Code: Select all
if (($post_id % 2) == 1) {
$table_color = "A1A1A1";
} else {
$table_color = "C9C9C9";
}
Your code in english:
if the remainder of $post_id divided by 2 equals 1 then the table color should be a1a1a1 otherwise it should be c9c9c9.
When you divide an odd number by 2, the remainder is 5, not 1.
Posted: Thu Mar 23, 2006 9:53 pm
by Todd_Z
i like to keep the colors in a css file:
Code: Select all
.row0 { background-color: #FFF; }
.row1 { background-color: #EEE; }
Code: Select all
while ( $obj = array_shift( $array ) ) {
echo "<tr class=\"row".($c=1-$c)."\">";
echo "<td>blahblahblah</td>";
echo "</tr>";
}
Posted: Thu Mar 23, 2006 10:34 pm
by feyd
agtlewis wrote:if the remainder of $post_id divided by 2 equals 1 then the table color should be a1a1a1 otherwise it should be c9c9c9.
When you divide an odd number by 2, the remainder is 5, not 1.
Bzzzz, does not compute.
The modulus operator (%) returns the remainder after division, not the quotient.
Posted: Thu Mar 23, 2006 11:20 pm
by mjmacarty
Bzzzz, does not compute.
The modulus operator (%) returns the remainder after division, not the quotient.
Yes, this is my understanding too. So if I didvide by 2 wouldn't that necessarily mean the remainder of an odd number is always 1?
Posted: Thu Mar 23, 2006 11:31 pm
by feyd
yes, mod two of a number will be zero for even, and one for odd.