Alternating table row colours

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

Post Reply
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

Alternating table row colours

Post by mjmacarty »

Pimptastic | Please use

Code: Select all

and

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

and

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]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Please use more informative topic titles

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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

Here is some more code

Post by mjmacarty »

feyd | Please use

Code: Select all

and

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

and

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]
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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>&nbsp;</td><td>$row[lastdate]</td><td>&nbsp;</td><td><a href=\"pageadmin.php?pagename=$row[pageid]\"><img src=\"edit.gif\" alt=\"Edit\" border=\"0\"></a>&nbsp;&nbsp;</td><td><a href=\"../home.php?pagename=$row[pageid]\" target=\"_blank\"><img src=\"view.gif\" alt=\"View\" border=\"0\"></a>&nbsp;&nbsp;</td><td><a href=\"{$PHP_SELF}?pagename=$row[pageid]&do=trash\"><img src=\"trash.gif\" alt=\"Trash\" border=\"0\"></a>&nbsp;&nbsp;</td><td><a href=\"{$PHP_SELF}?pagename=$row[pageid]&do=hide\"><img src=\"delete.gif\" alt=\"Hide\" border=\"0\"></a></td><td>&nbsp;&nbsp;&nbsp;<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 »

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>";
	
	}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Thanks very much pickle. I am just not sure why the code I wrote wouldn't work?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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>";
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
mjmacarty
Forum Commoner
Posts: 37
Joined: Tue Feb 21, 2006 3:20 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, mod two of a number will be zero for even, and one for odd.
Post Reply