Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Display comment tables in different colors

    I am preparing a comment system. Have no trouble with displaying comments.

    PHP Code:

    $classes 
    = array("blue","red","green","orange","yellow");

    include(
    "db.php");
    $query "SELECT ID,title,body,date FROM comments ORDER BY date DESC";
    $result mysql_query($query) or die(mysql_error());
    $num_comments mysql_num_rows($result);
    if(
    $num_comments == 0) {
        echo 
    '<p>No comments made yet.</p>';
    } else {
        echo 
    '<p>' $num_comments ' comments made.</p>';
        
    $i 0;
        while (
    $row mysql_fetch_array($result)) {
            echo 
    '<table id="comments" class="' $classes[$i] .'" cellspacing="0">';
            echo 
    '<tr>';
            echo 
    '<td valign="top">';
            echo 
    '<span>' $row['title'] . '</span>';
            echo 
    '<hr>';
            echo 
    '<p>' $row['body'] . '</p>';
            echo 
    '</td>';
            echo 
    '</tr>';
            echo 
    '</table>';
            echo 
    '<br>';
            
    $i++;
        }

    and external CSS file:

    Code:
    .blue {
    	background-color: blue;
    }
    
    .red {
    	background-color: red;
    }
    
    .green {
    	background-color: green;
    }
    
    .orange {
    	background-color: orange;
    }
    
    .yellow {
    	background-color: yellow;
    }
    I want the comment tables displayed in different colors and start over again when end of array is reached. In my case, display blue after yellow again.

    Any ideas?

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    put this at the beginning of your while loop:

    PHP Code:
    if($i count($classes))
    {
    $i '0';

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    This approach seems to work, may not be the best way though.
    I display the comments according to date so when a new comment is added, all comments change color again and single comments do not keep their color. Hope you get what I mean.


    PHP Code:
    if($classes[$i] == end($classes)) {
        
    $i 0;


    Final code:
    PHP Code:

    $classes 
    = array("blue","red","green","orange","yellow");

    include(
    "db.php");
    $query "SELECT ID,title,body,date FROM comments ORDER BY date DESC";
    $result mysql_query($query) or die(mysql_error());
    $num_comments mysql_num_rows($result);
    if(
    $num_comments == 0) {
        echo 
    '<p>No comments made yet.</p>';
    } else {
        echo 
    '<p>' $num_comments ' comments made.</p>';
        
    $i 0;
        while (
    $row mysql_fetch_array($result)) {
            echo 
    '<table id="comments" class="' $classes[$i] .'" cellspacing="0">';
            echo 
    '<tr>';
            echo 
    '<td valign="top">';
            echo 
    '<span>' $row['title'] . '</span>';
            echo 
    '<hr>';
            echo 
    '<p>' $row['body'] . '</p>';
            echo 
    '</td>';
            echo 
    '</tr>';
            echo 
    '</table>';
            echo 
    '<br>';
            
    $i++;
        if(
    $classes[$i] == end($classes)) {
            
    $i 0;
        }
        }


  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Quote Originally Posted by guvenck View Post
    This approach seems to work, may not be the best way though.
    I display the comments according to date so when a new comment is added, all comments change color again and single comments do not keep their color. Hope you get what I mean.


    PHP Code:
    if($classes[$i] == end($classes)) {
        
    $i 0;

    I don't really see a difference between my suggestion and your final code. If you got your thing to work, that's good!

    No matter which version of code you use, the coloring system is completely separate from the comments themselves, and it will will always color the first one blue, the second one red, and so on.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks for the help anyway. About the colors, you know, it never ends, we always want more For exp, I now try how to make the tables corners look round


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •