timmy0320 Posted March 26, 2008 Share Posted March 26, 2008 I'm having troubles with alternating my row bgcolor's on my table because of the way my while statement is being ran. I tried putting the if statement for $bg before the while statement and it just didn't work. Should I be using a trinary behavior for this? If so, could someone please simplify I'm not quite understanding on trinary statements. The result I am getting is {table} {row1 bg=ffffff} main category {row2 bg=ffffff} sub-cat of main {row3 bg=cccccc} main category {row4 bg=ffffff} main category {row5 bg=ffffff} sub-cat of main {row6 bg=ffffff} sub-cat of main and so on.... <?php function display_category_table($parent=0, $level=0) { $sql = "SELECT `category_id`, `name`, `description`, `count` FROM `blog_category` WHERE `parent` = '$parent' ORDER BY name"; $res = mysql_query($sql) or die(mysql_error()); $bg = "cccccc"; while (list($category_id, $name, $description, $count) = mysql_fetch_row($res)) { $indent = str_repeat('— ', $level); if ($bg == 'cccccc') { $bg = 'ffffff'; } else { $bg = 'cccccc'; } echo "<tr><td bgcolor=\"#$bg\" height=\"26\"><div align=\"center\">$category_id</div></td><td>$indent$name</td> <td>$description</td><td><div align=\"center\">$count</div></td><td><div align=\"center\"><a href=\"categories.php?action=edit&id=$category_id\">Edit</a></div></td> <td><div align=\"center\"><a href=\"categories.php?action=delete&id=$category_id\">Delete</a></div></td></tr>"; display_category_table($category_id, $level+1); } } ?> Usually when I start messing up silly shit like this it's time for me to get off the computer and take a break :\ Link to comment https://forums.phpfreaks.com/topic/97942-alternate-row-colors-on-table/ Share on other sites More sharing options...
ansarka Posted March 26, 2008 Share Posted March 26, 2008 Try this <?php function display_category_table($parent=0, $level=0) { $sql = "SELECT `category_id`, `name`, `description`, `count` FROM `blog_category` WHERE `parent` = '$parent' ORDER BY name"; $res = mysql_query($sql) or die(mysql_error()); $i=1; while (list($category_id, $name, $description, $count) = mysql_fetch_row($res)) { $i=$i*-1; $indent = str_repeat('&#8212; ', $level); if ($i>0) { $bg = 'ffffff'; } else { $bg = 'cccccc'; } echo "<tr><td bgcolor=\"#$bg\" height=\"26\"> <div align=\"center\">$category_id</div></td> <td>$indent$name</td> <td>$description</td><td><div align=\"center\">$count</div></td><td><div align=\"center\"><a href=\"categories.php?action=edit&id=$category_id\">Edit</a></div></td> <td><div align=\"center\"><a href=\"categories.php?action=delete&id=$category_id\">Delete</a></div></td></tr>"; display_category_table($category_id, $level+1); } } ?> Link to comment https://forums.phpfreaks.com/topic/97942-alternate-row-colors-on-table/#findComment-501112 Share on other sites More sharing options...
timmy0320 Posted March 26, 2008 Author Share Posted March 26, 2008 Exact opposite results. Link to comment https://forums.phpfreaks.com/topic/97942-alternate-row-colors-on-table/#findComment-501127 Share on other sites More sharing options...
wildteen88 Posted March 26, 2008 Share Posted March 26, 2008 Use an external counter: $i = 0; // initiate counter while (list($category_id, $name, $description, $count) = mysql_fetch_row($res)) { // alternate row colors $bg = ($i%2) ? 'ffffff' : 'cccccc'; $indent = str_repeat('— ', $level); echo "<tr><td bgcolor=\"#{$bg}\" height=\"26\"><div align=\"center\">$category_id</div></td><td>$indent$name</td> <td>$description</td><td><div align=\"center\">$count</div></td><td><div align=\"center\"><a href=\"categories.php?action=edit&id=$category_id\">Edit</a></div></td> <td><div align=\"center\"><a href=\"categories.php?action=delete&id=$category_id\">Delete</a></div></td></tr>"; display_category_table($category_id, $level+1); $i++; // increment counter } Link to comment https://forums.phpfreaks.com/topic/97942-alternate-row-colors-on-table/#findComment-501132 Share on other sites More sharing options...
timmy0320 Posted March 26, 2008 Author Share Posted March 26, 2008 Still having some issues, I believe it's the way my while statement is executing. I'll play with it for a while and see if I can figure this out. Link to comment https://forums.phpfreaks.com/topic/97942-alternate-row-colors-on-table/#findComment-501144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.