SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I just wrote a little message board ( as I just need a very basic one). I'm now looking for a way to make this "background-colour-change" effect for the thread output ( that the bg-colour is rotational on every second thread<-- hope this is the correct vocabulary).
    I thought this would be a good way:
    PHP Code:
    ($i 1$i <= $numberofthreads$i++)
    if 
    $i"odd number" {
    $bgcolour= ***
    } else {
    $bgcolour= ***;

    But how can I say "odd number"?
    Do you think it is a good way to solve it, thanx, Alex

  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)
    Try something like this:

    Code:
    $num = 1;
    
      while (whatever) {
        $color = ($num % 2 == 0) ? "#FFFFFF" : "#E9E9E9";
        printf('<td bgcolor="%s">Hi</td>
                ', $color);
        $num++;
      }

  3. #3
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, tried it but didn't work. But I'm sure I did something wrong. Could you explain this a little bit more ?

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it'd be easier for you to paste the exact code for your while loop (and the code slightly before and after it) -- I could then modify it, and explain the basics to you.

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Working with the code you gave you would do this as.

    PHP Code:
    ($i 1$i <= $numberofthreads$i++)
    if ( 
    $i == ) {
    $bgcolour= ***
    } else {
    $bgcolour= ***;

    % is the modulus operator. In the above code, what this does is divides $i (as an int) by 2 and returns the remainder (which logically with either be a 0 or 1 depending on whether $i is even/odd).

    TWTs code is eloquent (nice use of the ternary operator ) but may be confusing to you.

  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)
    Sid, things like that are what the ternary operator were created for. Anyway, if smorb would like me to explain the ternary operator, I should be able to accomodate -- but yeah, both ways work, the first is just a tad cleaner.

  7. #7
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanx to both of you. TWTCommish, I wanted to post my skript here yesterday but it seemed as there was a problem with this board here. If you don't mind please explain this ternary operater. I'm still learning.

  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)
    The ternary operator is shorthand for an if/else statement:

    condition ? value_if_true : value_if_false;

    Example: This code below...

    Code:
    if ($name == "Chris") {
      $authorized = "Yes";
    } else {
      $authorized = "No";
    }
    ...can be shortened to this:

    Code:
    $authorized = ($name == "Chris") ? "Yes" : "No";
    It tests the conditional ($name == "Chris"), and if it is true, $authorized is given the value of "Yes" -- if it is false, the value of "No" -- this is a very valuable way of assigning values.

    A real world example:

    You want to see if a user has entered their name, and then send them an email greeting:

    Code:
    $greeting = ($name != "") ? "Hello, $name!" : "Hello!";
    That will check to see if any data has been entered into the $name variable -- and if not, the $greeting variable will only contain "Hello" -- one line of code. Very handy!

    Now, try scrolling back up to my original code and see if it makes more sense to you. If not, just let me know what the problem is and I'll do my best to help you out.

  9. #9
    SitePoint Zealot daemon's Avatar
    Join Date
    Apr 2001
    Posts
    100
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or simply use something like the following:
    PHP Code:
    $c=0;
    while(...) {
        
    $c++;
        if(
    $c==0
            
    $bgcol="#FFFFFF";
        if(
    $c==1) {
            
    $bgcol="#EEEEEE";
            
    $c=0;    //NOTE: you reset $c and therefor only get values between 0 and 1
        
    }

    This is just the most simply way to do it.
    Hope this gives you some ideas of how to solve problems in an easy way.

  10. #10
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey daemon, I like easy solutions. As long as it runs without errors it's fine.
    Thanx again for helping.

  11. #11
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um, will that work? Looks to me like only the second conditional will return true -- every time.

    Edit: Yeah, ran it and got the second value everytime, sorry to say.
    Last edited by TWTCommish; May 11, 2001 at 08:06.

  12. #12
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    daemon needs to put $c++; into the ($c==0) conditional only.

    PHP Code:
    $c=0;
    while(...) {
        if(
    $c==0)
        {
            
    $bgcol="#FFFFFF";
            
    $c++;  // $c = 1; will have the same effect here.
        
    }
        else
        {
            
    $bgcol="#EEEEEE";
            
    $c=0;    //NOTE: you reset $c and therefor only get values between 0 and 1
        
    }

    The code is a bit clumsy, but it may be simple for a beginner to understand. Then again, it is simple to get it wrong as daemon demonstrated.
    Last edited by mmj; May 11, 2001 at 09:12.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  13. #13
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, I wrote that long post on the ternary operator for a reason smorb: whatever you use will work. If you have time, I recommend reading the post and thus becoming familiar with the ternary operator, so as to understand and use the more efficient version.

    If you don't have time, than the if/else will do. Good luck either way.

  14. #14
    SitePoint Addict
    Join Date
    Feb 2001
    Location
    Shanghai, China
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But why do you think it is more efficient to use this ternary operater ( you do not mean the time you save, that will be about 20 seconds, do you? ) and why does "mmj" say the code is a little bit clumsy ? Do you mean it will run faster ore what if you use those more "professional" ways?

  15. #15
    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, I havn't tested to see which way is truly faster in processing, but I suspect the ternary operator.

    Above all, though, even if the time difference is minimal, the code is much cleaner -- it's simply a good operator to use, as it saves space and clutter, and it will come in handy.

    If you want, I can run a quick benchmark test on the two.

  16. #16
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Here are the results of my test:

    If/Else:

    0.061432003974915 Seconds

    Ternary:

    0.00016093254089355 seconds

    A very large speed difference in PHP! So Now I'm always going with the ternary operator!

    EDIT: Pressing refresh gradually lowers the if/else for some reaosn yet the ternary stays roughly the same. But is always faster. Weird
    Last edited by petesmc; May 12, 2001 at 07:43.

  17. #17
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the benchmark -- I was just getting ready to do one of my own. Not surprising that it's faster. Quite frankly, even if it were a half a second slower, I'd probably use it. I hate too many brackets.

  18. #18
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just to let you know, this topic really got me going and I started benchmarking everything.

    Have a look here:

    http://forum.codingclick.com/showthr...p?threadid=190

  19. #19
    SitePoint Enthusiast
    Join Date
    May 2001
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's Chris though....INSANELY fond of the ternary operator.


  20. #20
    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 jackiemcghee
    that's Chris though....INSANELY fond of the ternary operator.

    You'd better believe it! I remember first reading about it -- I started to use it, and have gone nuts over it. I've saved a few dozen lines of code in a very short while.

  21. #21
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by petesmc
    Hi,

    Here are the results of my test:

    If/Else:

    0.061432003974915 Seconds

    Ternary:

    0.00016093254089355 seconds

    A very large speed difference in PHP! So Now I'm always going with the ternary operator!

    Holy...that's 381x faster!

  22. #22
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Strange though, every time you click refresh it gets smaller for the if/else. SO, everytime I want to refresh, I actually have to change a variable value in the script.

    They could be errors, but who knows...I'll test them again sometime and see what I come up with.

    -Peter


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
  •