SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2,4,6,8 or 1,3,5,7

    Hi guys,

    I want to print out a table consisting on an unknown amount of rows (based on a select query) and I want every second row to have the same colour. I was thinking that it would be a good idea to have an $i adding one for each result from the SELECT query, and then set the colour based on whether the $i was 2,4,6,8,10 or 1,3,5,7,9. But how can I determine this without having to write a huge if block?

    Thanks in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $color = ($i % 2 == 0) ? "#FFFFFF" : "#E9E9E9";
    $color will represent a white color if $i is an even number, and a light shade of gray if it is an odd number. Don't forget to increment <b>$i</b> in your loop somewhere.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I assume you have some sort of loop wrich goes through your results and writes them out? Well since what you really want to do is toggle between 2 colours, instead of using a variable which is incremented each record try using one that just swaps between two values.

    Say you called the variable $oddrow. Initialise it to true. Then inside your loop you can do a simple:

    PHP Code:
    if ($oddrow) {
        
    $colour "#000000";
        
    $oddrow false;
    }
    else {
        
    $colour "#FFFFFF";
        
    $oddrow true;

    This way, each time through the loop your $colour variable is set to be one colour or the other, and if you use that in your table row or whatever you end up with alternating colours.

    You could rewrite that code to get rid of the $oddrow variable like so:

    PHP Code:
    if ($colour "#000000") {
        
    $colour "#FFFFFF";
    }
    else {
        
    $colour "#000000";

    which has the same effect, but I just think the first way is clearer/easier to understand.

    Hope that helps .

    [Edit: or just do that ^ ]

  4. #4
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to you both. I went with Chris' suggestion, and it worked like a charm.
    Sincerely,

    Eirik Johansen
    Netmaking AS

  5. #5
    will code HTML for food Michel V's Avatar
    Join Date
    Sep 2000
    Location
    Corsica
    Posts
    552
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TWT, I couldn't find this way in the docs. Is it documented in an "exotic" section ?
    [blogger: zengun] [blogware contributor: wordpress]

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Search for information on the "ternary operator" -- either on PHP.net, or even on these forums. I, and several other members, have written posts describing the ternary operator -- I love it.

  7. #7
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    TWT's method uses two somewhat 'exotic' (depending on your background) features that are available in most C-style languages:

    The modulus operator (%), which performs an integer division and produces the remainder as a result. Thus, $integer % 2 will always equal 0 if $integer is even, and 1 if $integer is odd.

    The ternary operator (condition ? truevalue : falsevalue), which produces one of two values (truevalue or falsevalue), depending whether conditon is true or false.

    Thus, the command

    $color = ($i % 2 == 0) ? "#FFFFFF" : "#E9E9E9";

    Assigns the variable $color one of two values: "#FFFFFF" if the condition is true, or "#E9E9E9" if the condition is false. The condition in question is ($i % 2 == 0), or "$i is even".

    Make sense?

    Both of these operators are documented in the PHP manual.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  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)
    Well put Kevin -- yeah, I guess it is sort of exotic. I love 'em both, though. I love the fact that I can alternate colors and such with that one line of code. It's complex, yet elegant. The ternary operator is easily my favorite little "trick."

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    lalal
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to be a bit quicker ..
    PHP Code:
    $colour=(++$i&1)?'red':'blue'

  10. #10
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, the reference operator. I've heard of it. Could you explain it to me?

  11. #11
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's no reference -- that's a bitwise AND.

    Any odd number will have its lowest bit (in the binary representation of the number) set to 1, while any even number will have it set to 0.

    ANDing 1 with a number will always produce 1 if the lowest bit of the number is set to 1 (i.e. it is odd) and 0 if the lowest bit of the number is set to 0 (i.e. it is even). This can then be used as the condition of the ternary operator as above.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  12. #12
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure? I read that it was called the "reference operator" on PHP.net. Is it possible it could be called that without actually referring to a reference?

  13. #13
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's the reference operator as well. Just depends on the context. The '-', for example, can be the unary operator (makes a single value negative) or the standard subtraction operator.

    In this context, & is a bitwise AND.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  14. #14
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. Makes sense now. Thank you.


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
  •