SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    alternating table row colors

    i have a script which displays a list of names and stats from a database in a table.

    right now i have it set up to display 11 names per page. i figured that it would be easier on the eyes of the viewer if every other block of data had a different table background color.

    what's the best way to go about doing this?
    i have a while loop which will print out the records from the db in a table (11 per page) as long as data exist, but i can't figure out how to make it alternate between the records.

    has anyone done this before?
    thanks.
    . . . chris

  2. #2
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just looking at my code for that a few minutes ago

    $cellBGN="#FFFFFF";
    ($cellBG==$cellBGN ? $cellBG="#CCCCCC" : $cellBG="#FFFFFF");



    echo ( "<tr bgcolor=$cellBG><td>*Info*</td></tr>";

  3. #3
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make a var (lets say $x)

    then do a loop, check if $x is even or odd. even prints one color, oddprints the other.

    if($x % 2 =0)
    {
    //color 1
    }
    else
    {
    //color 2
    }


    then add 1 to $x with $x++;

  4. #4
    SitePoint Enthusiast mandi_nole's Avatar
    Join Date
    Jun 2001
    Location
    Orlando, FL
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that code, dude - I've been wondering how to replace my Mod function from ASP...

    It's cool to watch masters at work...

    Mandi
    Love ya - mean it!

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by slighltywhacked
    Make a var (lets say $x)

    then do a loop, check if $x is even or odd. even prints one color, oddprints the other.

    if($x % 2 =0)
    {
    //color 1
    }
    else
    {
    //color 2
    }


    then add 1 to $x with $x++;
    Actually, I don't know if that will work -- I believe you need two equals signs (=) to compare values. Anyway, it's a bit cleaner to use this:

    Code:
    $color = ($x % 2 == 0) "white" : "red";

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very cool
    BTW, shouldn't it be
    PHP Code:
    $color = ($x == 0) ? "white" "red"

  7. #7
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, the single = sign will work here. I use it all the time... Normaly u would use the == instead though, = usualy asigns a value and the == compares vallues

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I get for staying up until 2AM. Thanks Taker

  9. #9
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    scratch that.. u do have to use ==

    little out of it here

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by TWTCommish
    That's what I get for staying up until 2AM. Thanks Taker
    Last edited by thetakerfan; Jul 6, 2001 at 13:08.

  11. #11
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so i used manipura's code and it worked perfectly. thanks alot.

    could someone please explain either one of these 2 pieces of code for me?

    PHP Code:
    $color = ($x == 0"white" "red"
    i was trying to find some info on using % and how it relates to the rest of the code, but i found nothing in the php manual.

    likewise, i can't seem to wrap my brain around the following piece. it seems somewhat similar to the one above with the
    PHP Code:
    $var == $var $var ="somthing" $var="somthing" 
    but i just don't get what exactly is going on there.

    PHP Code:
    ($cellBG==$cellBGN $cellBG="#CCCCCC" $cellBG="#FFFFFF"); 
    anybody care to endulge?
    . . . chris

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok,
    PHP Code:
    $color = ($x == 0) ? "white" "red"
    straight from the php manual, the % is:
    Remainder of $a divided by $b
    or in this case, $x divided by 2.
    if the remainder of x / 2 is 0, meaning an even number, then $color will be white, if not, $color will be red.
    basically, if
    PHP Code:
    ($x == 0
    is true, then what comes after the "?" is used, if its not, then whats after the ":" is used

  13. #13
    SitePoint Enthusiast tipiyanos's Avatar
    Join Date
    Jun 2001
    Location
    NY, New York
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is an alternate form of if/else statement.

    PHP Code:
    ($cellBG==$cellBGN $cellBG="#CCCCCC" $cellBG="#FFFFFF"); 
    Here first it checks whether $cellbg==$cellbgn. if true it follows the first condition ie. assigns #cccccc to $cellbg. if false it follows the other part.
    written in the conventional form it would be
    PHP Code:

    if ($cellBG==$cellBGN) {
    $cellBG="#CCCCCC";
    }

    else {
    $cellBG="#FFFFFF");

    Hope that helps.

  14. #14
    SitePoint Addict
    Join Date
    Dec 2000
    Location
    BOSTON MA
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for all the info. it's definitely helpful to have things explained so clearly.

    one more question. if i was using this line :
    PHP Code:
    $color = ($x == 0"white" "red"
    do i declare what $x is at any point before it? or is it basically a placeholder?

    thanks again.
    . . . chris

  15. #15
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by manipura

    $cellBGN="#FFFFFF";
    ($cellBG==$cellBGN ? $cellBG="#CCCCCC" : $cellBG="#FFFFFF");
    I like using shorter term for things as easy as this, saves time. I think everyone should work on making things short. Like instead onf <?php echo"$var"; ?> Just go <?=$var?>

    I also love keyboard shortcuts!
    alt+f+o (open file)
    alt+f+s (save file)

    Or easy stuff like in Internet explorer
    F4 =Highlights Address Bar
    F5 = refesh
    F6 = Selects different frames
    F11 = View Fullscreen!

  16. #16
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do i declare what $x is at any point before it? or is it basically a placeholder?
    Before your while loop, declare $x to be 0, or 1, or whatever you like, then inside your while loop, at the end say, put $x++ to increment the $x variable by one.

    And make sure you use the code WITH the ?
    PHP Code:
    $color = ($x == 0) ? "white" "red"

  17. #17
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by manipura


    I like using shorter term for things as easy as this, saves time. I think everyone should work on making things short. Like instead onf <?php echo"$var"; ?> Just go <?=$var?>
    .....
    Does that work if you do not have short tags on?
    I do, but I always code so it will work without short tags.

  18. #18
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $cellBGN="#FFFFFF";
    ($cellBG==$cellBGN ? $cellBG="#CCCCCC" : $cellBG="#FFFFFF");
    I'm confused...don't you need a variable there to catch the result? I also don't see how the conditional will alternate the color. I also don't see how the parentheses are around the whole thing.

  19. #19
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is what I think
    PHP Code:
    $cellBGN="#FFFFFF"
    (
    $cellBG==$cellBGN $cellBG="#CCCCCC" $cellBG="#FFFFFF"); 
    is supposed to do, even though it should atleast be like this
    PHP Code:
    $cellBGN="#FFFFFF"
    (
    $cellBG==$cellBGN) ? $cellBG="#CCCCCC" $cellBG="#FFFFFF"
    and actually, perhaps this is easier, depending on what you like
    it first sets $cellBG to #FFFFFF. so then, $cellBG = $cellBGN, and so the color is #CCCCCC. Now on the next row, since $cellBG is not equal to $cellBGN, $cellBG is now #FFFFFF, and that is what is used. so it does alternate the color thats used, just in a weird way. I don't think I'd have ever thought of doing it that way

  20. #20
    SitePoint Enthusiast
    Join Date
    Apr 2001
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this would work too wouldn't it?
    PHP Code:
    $cellBGN="#FFFFFF"
    $cellBG = ($cellBG==$cellBGN) ? "#CCCCCC" "#FFFFFF"

  21. #21
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by thetakerfan
    very cool
    BTW, shouldn't it be
    PHP Code:
    $color = ($x == 0) ? "white" "red"
    There is one small logic erro with the above. You need to increment the counter:
    PHP Code:
    $color = ($x++ % == 0) ? "white" "red"
    PHP Code:
    $cellBGN="#FFFFFF";*
    $cellBG*
    =*($cellBG==$cellBGN)*?*"#CCCCCC"*:*"#FFFFFF"
    I had never thought of toggling a value in this manner. It's very neat!

  22. #22
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sid: I was presenting it as an alternative to the if/then structure, and as such figured that the rest that was said (about incrementing and such) would be a given. Yeah, that method is interesting...surprised you havn't seen it before, though. I've seen it once...on these same forums, of course.


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
  •