Jump to content

Alternate row colors on table


timmy0320

Recommended Posts

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('&#8212; ', $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

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('&#38;#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);
                    }
        }
?>

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('&#8212; ', $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
                    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.