Auto switching ON/OFF

by manhal
3 replies
I like to use a php function to switch between ON and OFF automatically upon the call.
Of course something simple like

$condition="OFF";
switch ($condition) {
case "OFF" :
$condition="ON";
break;
case "ON" :
$condition="OFF";
break;
}

this always returns ON but that is not what I need. How can I make it so that it remember the previous setting of $condition and act accordingly so it alternate between ON/Off automatically upon the call to the function?

Thanks
#auto #on or off #switching
  • Profile picture of the author webpro4hire
    Manhal,

    The reason the return is always 'ON' is because you set condition = 'OFF', hence the case: 'OFF' is always selected.

    What you need for this is either a parameter passed in the calling URL OR setting a cookie. Upon calling the script,
    get your condition from the parameter (or cookie).

    if ($_COOKIE['condition'] == 'ON') {
    setcookie('condition','OFF',time()+86400, '/', '.yourdomain.com');
    }
    else {
    setcookie('condition','ON',time()+86400, '/', '.yourdomain.com');
    }


    Cheers,
    WP4H
    {{ DiscussionBoard.errors[3195107].message }}
  • Profile picture of the author Ken Durham
    Originally Posted by manhal View Post

    I like to use a php function to switch between ON and OFF automatically upon the call.
    Of course something simple like

    ="OFF";
    switch () {
    case "OFF" :
    ="ON";
    break;
    case "ON" :
    ="OFF";
    break;
    }

    this always returns ON but that is not what I need. How can I make it so that it remember the previous setting of and act accordingly so it alternate between ON/Off automatically upon the call to the function?

    Thanks
    If this call is per user, then perhaps use sessions
    PHP Tutorial - Session

    If you need it "per call", no matter who is doing the calling, then you're going to have to write some sql
    Signature

    yes, I am....

    {{ DiscussionBoard.errors[3196672].message }}
  • Profile picture of the author manhal
    thank you both.
    sessions worked fine.
    {{ DiscussionBoard.errors[3216004].message }}

Trending Topics