Page 1 of 1
alternating row colors...
Posted: Thu Jan 04, 2007 1:40 pm
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.
Posted: Thu Jan 04, 2007 1:44 pm
by jayshields
Please use the search function of the forum, every way possible has been covered atleast once before.
Posted: Thu Jan 04, 2007 1:50 pm
by boo_lolly
i DID search the forums, but it brought me 6000+ matches...
Posted: Thu Jan 04, 2007 1:51 pm
by Kieran Huggins
sounds like plenty of helpful posts to me!
Did you want more?
Posted: Thu Jan 04, 2007 1:53 pm
by jayshields
Posted: Thu Jan 04, 2007 1:57 pm
by feyd
search for Zebra.
Posted: Thu Jan 04, 2007 2:09 pm
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";
}
?>
Posted: Thu Jan 04, 2007 2:12 pm
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!
Posted: Thu Jan 04, 2007 4:03 pm
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.
Posted: Thu Jan 04, 2007 4:05 pm
by Kieran Huggins
ok, you still have an extra </a>, btw
Posted: Thu Jan 04, 2007 4:38 pm
by boo_lolly
Kieran Huggins wrote:ok, you still have an extra </a>, btw
no i dont... where?
Posted: Fri Jan 05, 2007 12:23 am
by dibyendrah
feyd wrote:search for Zebra.
Yes. Zebra tables are cool.
Code: Select all
http://alistapart.com/articles/zebratables/
Posted: Fri Jan 05, 2007 1:16 am
by Kieran Huggins
years ago I wrote a javascript zebrafier:
http://kieran.ca/code/zebraStripe/
Maybe it will be useful to you