Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Row Color

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Row Color

    I just finished up a script that displays all users in a database and noticed that it is hard to read because every row looks the same. So I was hoping that there is a way to have the scrip assign a color (through a css class) to each row so that it can alternate colors. Here is how I am echoing the the lines of users:
    PHP Code:
    echo
        <tr>
            <td>'
    .$username.'</td>
            <td>'
    .$email.'</td>
            <td>'
    .$signup.'</td>
            <td>'
    .$ip.'</td>
            <td><a href="index.php?act=chk_delete&id='
    .$id.'">[X]</a></td>
            <td><a href="index.php?act=edit&id='
    .$id.'">Edit</a></td>
            <td><a href="index.php?act=chk_ban&id='
    .$id.'">Ban</a></td>
        </tr>'
    ;

    Thanks for any help

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Well, what I think you want to know is this: http://alistapart.com/articles/zebratables

    I am kind of confused about how you're echoing them, though - does $username, for example, echo ALL the usernames at once?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That is what I want to do, but have the php do it automatically. I forgot a part of the code, here is the full thing:
    PHP Code:
    while($row mysql_fetch_array($result)) {
        
    extract($row);

    echo

        <tr>
            <td>'
    .$username.'</td>
            <td>'
    .$email.'</td>
            <td>'
    .$signup.'</td>
            <td>'
    .$ip.'</td>
            <td><a href="index.php?act=chk_delete&id='
    .$id.'">[X]</a></td>
            <td><a href="index.php?act=edit&id='
    .$id.'">Edit</a></td>
            <td><a href="index.php?act=chk_ban&id='
    .$id.'">Ban</a></td>
        </tr>'
    ;
    }

    echo 

    The while(mysql_fetch_array($result) { section tells it to do whatever is specified as long as there is another result. Thanks for the help so far

  4. #4
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone know how to have php automatically assign a class to each row?

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Are you talking about alternating row colors for each row (similar to the one I have here)? If so, the code is actually pretty simple. Don't have time to post now, but will do it first thing tomorrow morning.
    Last edited by thetestingsite; 01-19-2007 at 03:41 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    As promised, this is the code I used to make the rows alternate colors (I took the liberty of integrating it with the code you posted above):

    Code:
    $count = 1;
    	
    	$color = "#EEEEEE";
    while($row = mysql_fetch_array($result)) {
        extract($row);
    
    echo' 
        <tr style="background-color: '.$color.'">
            <td>'.$username.'</td>
            <td>'.$email.'</td>
            <td>'.$signup.'</td>
            <td>'.$ip.'</td>
            <td><a href="index.php?act=chk_delete&id='.$id.'">[X]</a></td>
            <td><a href="index.php?act=edit&id='.$id.'">Edit</a></td>
            <td><a href="index.php?act=chk_ban&id='.$id.'">Ban</a></td>
        </tr>';
    }
    
    ++$count;
    		if ($color == "#EEEEEE") {
    			$color = "#CCCCCC";
    		} else {
    			$color = "#EEEEEE";
    		}	
    }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That is exactly what I want to do, but when I tried it, it assigned the same color to all the rows. Did you mistype something maybe, or is it something I messed up? I don't understand this part:
    PHP Code:
    if ($color == "#EEEEEE") {
                
    $color "#CCCCCC";
            } else {
                
    $color "#EEEEEE";
            } 
    Isn't the first part saying that if $color = #EEEEEE, make it #CCCCCC?

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I think I know what's wrong with it. In the code I posted above, I added an extra bracket (probably from copying and pasting from your posted code). Here's how it should look:

    Code:
    $count = 1;
    	
    	$color = "#EEEEEE";
    while($row = mysql_fetch_array($result)) {
        extract($row);
    
    echo' 
        <tr style="background-color: '.$color.'">
            <td>'.$username.'</td>
            <td>'.$email.'</td>
            <td>'.$signup.'</td>
            <td>'.$ip.'</td>
            <td><a href="index.php?act=chk_delete&id='.$id.'">[X]</a></td>
            <td><a href="index.php?act=edit&id='.$id.'">Edit</a></td>
            <td><a href="index.php?act=chk_ban&id='.$id.'">Ban</a></td>
        </tr>';
    
    
    ++$count;
    		if ($color == "#EEEEEE") {
    			$color = "#CCCCCC";
    		} else {
    			$color = "#EEEEEE";
    		}	
    }
    Quote Originally Posted by Titan85
    Isn't the first part saying that if $color = #EEEEEE, make it #CCCCCC?
    Yes, you are correct. This is what changes the colors of the cells.

    Sorry I can't further explain it at this time, but perhaps someone else could.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I found the problem, it was that I put the code outside of my while statement. So it was my fault

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Glad you found the problem and got it resolved. Also, glad it is working for you. Let me know if you need any more help.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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