SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Member metabeat's Avatar
    Join Date
    Mar 2002
    Location
    Spain
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking TOGGLE... alternating row colours, HOW??

    Ok I'm pulling some rows from MySql, and I'm putting each row in a HTML table row. I basically want to alternate the rows' background colour, so row one will be for ex. blue, row two green, row three blue again, and so on...

    I've set up variables for all the bgcolor tags, but what I can't figure out is how to toggle these variables between two value sets every time my while loop runs.

    What I tried is:
    Code:
    	
    if($toggle=1):
    $toggle=2;
    $dkcolour = "#669900";
    $ltcolour = "#99FF00";
    $hr = $hrclass[1];
    				
    elseif($toggle=2):
    $toggle=1;
    $dkcolour = "#006666";
    $ltcolour = "#0099FF";
    $hr = $hrclass[1];
    
    endif;
    It should be obvious why this doesn't work, the code checks for the toggle variable and toggles it, but then I'd need it to jump tp the next section and ignore the elseif statement (which just sets it back to 1)... I'm a newbie to programming in general so maybe there's a completely more intelligent way to do this....

    Thx!

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    CA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your if() statements, you need to change "=" to "==". Otherwise, you're always setting $toggle to either 1 or 2, which always evaluates to TRUE. (You are using PHP, right?)

    You can also use the modulus operator to test whether your toggle variable is even or odd:

    PHP Code:
    $i 0;
    while(...)
    {
    ...
    $dkcolour = ($i%2) ? "#669900" "#006666";
    $ltcolour = ($i%2) ? "#99ff00" "#0099ff";
    $i++;
    ...

    Jon Sagara
    Y.A.P.P.

  3. #3
    SitePoint Member metabeat's Avatar
    Join Date
    Mar 2002
    Location
    Spain
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, thx alot, changing to == did the trick. Is there any reason why I would want to do it the other way? I mean, is it a better way to code it or does it amount to the same thing?

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    CA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fewer instructions. The end result is the same.
    Jon Sagara
    Y.A.P.P.

  5. #5
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the ternary operator method also speeds up the script, but the reason I use it simply 'cos it's one line.
    Work smarter, not harder. -Scrooge McDuck

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * looks in JS forum ... looks here ... *

    Similarities i see!

    There was a large post in JS (top of the forum)
    which harped on about this kind of thing... maybe
    the two overlap a little


    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    SitePoint Member metabeat's Avatar
    Join Date
    Mar 2002
    Location
    Spain
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would someone be so kind as to explain the actual funciton of the 'modulus' operator. I want to incorporate this method in my code but i hate to write code i don't actually understand!

    Thx!

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    CA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The modulus operator (%) returns the remainder of a division operation.

    For example, 5/2 = 2 remainder 1, right? % returns the 1 of that division operation.
    Jon Sagara
    Y.A.P.P.

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2002
    Location
    CA, USA
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forgot this part:

    In our case, dividing any number by two and looking at the remainder will tell you whether or not that number is odd or even:

    1%2 = 1
    2%2 = 0
    3%2 = 1
    4%2 = 0
    5%2 = 1

    ...

    and on and on and on...
    Jon Sagara
    Y.A.P.P.


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
  •