PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mjmacarty
Forum Commoner
Posts: 37 Joined: Tue Feb 21, 2006 3:20 pm
Post
by mjmacarty » Thu Mar 23, 2006 11:26 am
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]
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Thu Mar 23, 2006 11:28 am
Please use more informative topic titles
Thanks
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 23, 2006 11:32 am
it'd help to understand how you were using $table_color by post a bit more code.
mjmacarty
Forum Commoner
Posts: 37 Joined: Tue Feb 21, 2006 3:20 pm
Post
by mjmacarty » Thu Mar 23, 2006 12:59 pm
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]
mattcooper
Forum Contributor
Posts: 210 Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK
Post
by mattcooper » Thu Mar 23, 2006 2:10 pm
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!
mjmacarty
Forum Commoner
Posts: 37 Joined: Tue Feb 21, 2006 3:20 pm
Post
by mjmacarty » Thu Mar 23, 2006 2:44 pm
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>";
}
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu Mar 23, 2006 3:37 pm
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mjmacarty
Forum Commoner
Posts: 37 Joined: Tue Feb 21, 2006 3:20 pm
Post
by mjmacarty » Thu Mar 23, 2006 8:18 pm
Thanks very much pickle. I am just not sure why the code I wrote wouldn't work?
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Mar 23, 2006 8:24 pm
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.
Todd_Z
Forum Regular
Posts: 708 Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan
Post
by Todd_Z » Thu Mar 23, 2006 9:53 pm
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>";
}
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 23, 2006 10:34 pm
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.
mjmacarty
Forum Commoner
Posts: 37 Joined: Tue Feb 21, 2006 3:20 pm
Post
by mjmacarty » Thu Mar 23, 2006 11:20 pm
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Mar 23, 2006 11:31 pm
yes, mod two of a number will be zero for even, and one for odd.