alternating row colors...

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
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

alternating row colors...

Post by boo_lolly »

I have a while loop that outputs an HTML table. i need iit to alternate row colors. i'm sure there's many ways to skin this cat, but how would i incorporate it within the provided code below?

Code: Select all

<?php
	while($s = mysql_fetch_array($sQuery)){
		echo "\t<tr>\n".
                     "\t\t<td>". $s[id] ."</td>\n".
                     "\t\t<td>". $s[name] ."</td>\n".
                     "\t\t<td align=\"right\"><p><a href=\"index.php?site=". $site ."&zone=". $zone ."&action=manu&editmanu=true&id=". $s[id] .">\n".
		     "<img src=\"images/button_edit.gif\" border=\"0\" alt=\"Edit\"></a>\n".
                     "<a href=index.php?site=". $site ."&zone=". $zone ."&action=manu&delete=true&id=". $s[id] .">\n".
		     "<img src=\"images/button_delete.gif\" border=\"0\" alt=\"Delete\"></a></p>\n".
                     "\t\t</td>\n".
        	     "\t</tr>\n";
	}
?>
many thanks.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Please use the search function of the forum, every way possible has been covered atleast once before.
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i DID search the forums, but it brought me 6000+ matches...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

sounds like plenty of helpful posts to me!

Did you want more?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

What about Google. It gave me this: http://codewalkers.com/tutorials/6/2.html
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search for Zebra.
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Something like this maybe?

Code: Select all

<?php
        while($s = mysql_fetch_array($sQuery)){
               
               
            if (empty($key)) {
            	$key=0;
               if ($s[$key] % 2 ==0) {
               		$color='#ffffff';
               	} elseif ($key % 2 !=0) {
               	    $color='#000000';
               	}
             }
               
               
               echo "\t<tr bgcolor=" . $color . ">\n".
                     "\t\t<td>". $s[id] ."</td>\n".
                     "\t\t<td>". $s[name] ."</td>\n".
                     "\t\t<td align=\"right\"><p><a href=\"index.php?site=". $site ."&zone=". $zone ."&action=manu&editmanu=true&id=". $s[id] .">\n".
                     "<img src=\"images/button_edit.gif\" border=\"0\" alt=\"Edit\"></a>\n".
                     "<a href=index.php?site=". $site ."&zone=". $zone ."&action=manu&delete=true&id=". $s[id] .">\n".
                     "<img src=\"images/button_delete.gif\" border=\"0\" alt=\"Delete\"></a></p>\n".
                     "\t\t</td>\n".
                     "\t</tr>\n";
        }
?>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

A couple of pointers:

Your code is terribly difficult to read, which is why you likely missed the first closing anchor tag. Instead, try this method:

Code: Select all

while($s = mysql_fetch_array($sQuery)){
?>
	<tr>
		<td><?=$s['id']?></td>
		<td><?=$s['name']?></td>
		<td align="right"><p><a href="index.php?site=<?=$site?>&zone=<?=$zone?>&action=manu&editmanu=true&id=<?=$s['id']?>">
		....		
<?
}
Also, when inserting short snippets of HTML tags, I usually use single quotes like so:

Code: Select all

echo '<a href="somewhere.php">I can use "double quotes" without escaping them!</a>'
On the subject of zebra striping, it would be best to use CSS classes for this.

Good hunting!
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i got it. it was pretty simple...

Code: Select all

<?php
	$color = 0;
	while($s = mysql_fetch_array($sQuery)){
		if($color % 2 == 0){
			$alt_color = "FFFFFF";
		}else{
			$alt_color = "DDDDDD";
		}
		echo "\t<tr bgcolor=\"". $alt_color ."\">\n".
                     "\t\t<td>". $s[id] ."</td>\n".
                     "\t\t<td>". $s[name] ."</td>\n".
                     "\t\t<td align=\"right\"><p><a href=\"index.php?site=". $site ."&zone=". $zone ."&action=manu&editmanu=true&id=". $s[id] .">\n".
		     "<img src=\"images/button_edit.gif\" border=\"0\" alt=\"Edit\"></a>\n".
                     "<a href=index.php?site=". $site ."&zone=". $zone ."&action=manu&delete=true&id=". $s[id] .">\n".
		     "<img src=\"images/button_delete.gif\" border=\"0\" alt=\"Delete\"></a></p>\n".
                     "\t\t</td>\n".
        	     "\t</tr>\n";
		$color++;
	}
?>
i don't use single quotes (') for strings because if there is a syntax error it is MUCH more difficult to diagnose. sure, maybe my code looks a little cluttered, but i like to escape (\) all of my double quotes and escape the string (". $var .") whenever i have a variable to print, because it's much easier for me to make sure it's not a syntactical error. i can make sure every variable is being printed. and i know what is being interpereted as a string, and what is not. anyways, the source code that is outputted looks just as good. i really don't like escaping the php header (<?php ?>) tags just for a few lines of HTML. and then dropping back into php tags to add a bracket or whatever.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

ok, you still have an extra </a>, btw
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

Kieran Huggins wrote:ok, you still have an extra </a>, btw
no i dont... where?
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

feyd wrote:search for Zebra.
Yes. Zebra tables are cool.

Code: Select all

http://alistapart.com/articles/zebratables/
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

years ago I wrote a javascript zebrafier:

http://kieran.ca/code/zebraStripe/

Maybe it will be useful to you
Post Reply