www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Switch & Session

  1. #1
    Join Date
    Jun 2008
    Posts
    118

    resolved [RESOLVED] Switch & Session

    OK here's the code

    PHP Code:

    switch (true){

    case (
    $_POST['cardType']=='recognize'):
        
        
    //$card_id=$_POST['card_id'];
        
    $_SESSION['card1']=$_POST['card_id'];
        echo 
    $_SESSION['card1'];
        
    $table=display_cards('realize');

    break;


    case (
    $_POST['cardType']=='realize'):
        
        
    //$card_id=$_POST['card_id'];
        
    $_SESSION['card2']=$_POST['card_id'];
        echo 
    $_SESSION['card2'];
        
    $table=display_cards('reconnect');

    break;


    case (
    $_POST['cardType']=='reconnect'):
        
        
    //$card_id=$_POST['card_id'];
        
    $_SESSION['card3']=$_POST['card_id'];
        echo 
    $_SESSION['card3'];
        
    $table=display_cards('receive');

    break;


    case (
    $_POST['cardType']=='receive'):
        
        
        
    $_SESSION['card4']=$_POST['card_id'];
        echo 
    $_SESSION['card2'];
        echo 
    $_SESSION['card4'];
        
    break;


    default:
        
        
    session_start();
        
    $_SESSION['card1']='';
        
    $_SESSION['card2']='';
        
    $_SESSION['card3']='';
        
    $_SESSION['card4']='';
        
        
    $table=display_cards('recognize');

    break;

    It seems that this code resets the session variables every time I run it, so I end up only with the last one that was set.

    I just can't understand why, as the default shouldn't run if one of the others is true

  2. #2
    Join Date
    Jun 2008
    Posts
    118
    OK

    I tried using
    PHP Code:
    elseif 
    but it still wipes out all the variables except the last one set

  3. #3
    Join Date
    Apr 2009
    Posts
    3

    Get the White Out

    Perhaps your POST variables contain whitespace (assuming their content came from some sort of interface). Try:
    PHP Code:
    case (trim($_POST['cardType'])=='reconnect'): 
    or
    PHP Code:
    case (strstr($_POST['cardType'], 'reconnect') !== false): 
    or for case-insensitive case
    PHP Code:
    case (stristr($_POST['cardType'], 'reconnect') !== false): 
    Also, try applying the following test to your POST data:
    PHP Code:
    isset($_POST['cardType']) 
    BTW: Have you looked at the POST data to see if the strings you are testing for are really there?

    PHP Code:
    echo "\$_POST['cardType']: ***{$_POST['cardType']}***"
    [the stars ('***') are to make whitespace stand out.]

    Also, are you sure you have the case right on your key: 'cardType'? Perhaps the following test will reveal something:
    PHP Code:
    var_dump($_POST); 
    Last edited by steve42lawson; 04-15-2009 at 11:27 AM. Reason: Makin' it prettier [just a nubie who noticed PHP tags too late]

  4. #4
    Join Date
    Jun 2008
    Posts
    118
    I've found a problem.

    Basicaly, whenever I was writing my code, I would declare a session only in "default" part of the switch statement. This ment that whenever page loaded with any data POSTED to it, the session was not declared and the variables seem to not have been saved.

    The solution seems to be writing session_start() in every instance of the "switch" statement.

  5. #5
    Join Date
    Apr 2009
    Posts
    3

    Question One session_start(); ought to do it

    Doh! Didn't even notice that -- so what if you place the session_start(); call before the switch statement [preferably at the top of the PHP block, before any other PHP code]? Does that eliminate the need for the multiple session_start(); calls within the switch statement?

  6. #6
    Join Date
    Jun 2008
    Posts
    118
    There's been some problem with it, but I think it's down to the fact that I was declaring session variable on the top as well, so one would have to declare them in the "default" part of the switch statement

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles