Jump to content

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
                    }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.