SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Odd behaviour with the switch statement...

    I'm wanting to add a new index to the $_SESSION['fsb']['order'] array, and based on the action selected by the user in the form, it does that just fine in the case of "$stuff['formButtons']['addABag']", however, even though the new index is added successfully to the session array, the line highlighted in red under the "default" case, STILL executes wiping out the new index that I just created. I know this, because I printed out the array before the "break" on the proper case, and after the close of the switch. Before the break, the array was properly populated; after the close of the switch, the new index that I had just seen, was gone, and the $_SESSION['fsb']['order'] was empty.

    Then, I commented out the red line in the default case and the session was properly populated outside of the switch. One other interesting thing, the line, "$bagNum = 1;" is not executed, even though the line above it is.

    Can anyone see why this statement would be executed, or if I have something in my code that is causing this that I am overlooking?


    Code:
      switch($action)
      {
        case $stuff['formButtons']['oops']:
          header('Location:'.$stuff['page']['stepTwo']);
          exit;
          break;
          
        case $stuff['formButtons']['lastBag']:
        case $stuff['formButtons']['changes']:
          addBagToSession($_POST, $bagNum);
          header('Location:'.$stuff['page']['stepTwo']);
          exit;
          break;
          
        case $stuff['formButtons']['addABag']:
          if(substr($_SERVER['HTTP_REFERER'], strlen($stuff['page']['stepOne'])*-1) == substr($thisPage, strlen($stuff['page']['stepOne'])*-1)) // This is so we don't wipe out the bag when adding from the review screen
          {
            $_SESSION['fsb']['order'][$bagNum] = addBagToSession($_POST);
            $bagNum++;
          }
          $_SESSION['fsb']['bagNum'] = $bagNum;
          break;
          
        case $stuff['formButtons']['edit']:
          $bagNum = $_POST['bagNum'];
          break;
          
        case $stuff['formButtons']['remove']:
          removeFromOrder($_POST['bagNum']);
          header('Location:'.$stuff['page']['stepTwo']);
          exit;
          break;
          
        default:
          $_SESSION['fsb']['order'] = array();
          $bagNum = 1;
          break;
      }
    John

  2. #2
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's in $action?
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  3. #3
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var_dump() the $action variable and all the other arrays that are contained in the cases to give you a better of idea of what it's doing.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  4. #4
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dachande663 View Post
    What's in $action?
    $action is the name of the "submit" button. It's value is based on the condition of the form, and which "submit" button is pressed.

    Its values can be:

    "Add a bag" = $stuff['formButtons']['addABag']
    "Save these changes" = $stuff['formButtons']['changes']
    "No more bags" = $stuff['formButtons'][oops']
    "This is the last bag" = $stuff['formButtons']['lastBag']

    etc...
    John

  5. #5
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wildhoney View Post
    var_dump() the $action variable and all the other arrays that are contained in the cases to give you a better of idea of what it's doing.
    Here is the var_dump of $action and $stuff['formButtons']

    Code:
    string(15) "Add another bag"
    array(9) {
      ["save"]=>
      string(21) "Save this information"
      ["addABag"]=>
      string(15) "Add another bag"
      ["changes"]=>
      string(18) "Save these changes"
      ["confirm"]=>
      string(15) "Place the order"
      ["edit"]=>
      string(13) "Edit this bag"
      ["lastBag"]=>
      string(20) "This is the last bag"
      ["oops"]=>
      string(12) "No more bags"
      ["remove"]=>
      string(15) "Remove this bag"
      ["send"]=>
      string(4) "Send"
    }
    Why would I have mulitples of "string(15)"? Especially in the array?
    John

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    var_dump() explains what type is contained in an array along with the values.

    print_r() shows just the values.

    I use var_dump() especially if I suspect that some weird int to string conversion has happened, or I am not sure if I have a zero, null, or empty (not set) value, can also come in handy if you want to investigate character encoding problems. Most of the time I use print_r() **.
    PHP Code:
      ["send"]=>
      
    string(4"Send" 
    4 character string = "Send"

    ** Even better when used with Xdebug

  7. #7
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! I didn't know that about var_dump (I usually use print_r(), also).
    John


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
  •