www.webdeveloper.com
Results 1 to 4 of 4

Thread: sessions in php

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Posts
    23

    sessions in php

    I was trying to append some values in already exists session.

    Code:
    if(isset($_SESSION['ems_id']) == NULL) {
        echo "empty";
        $_SESSION['ems_id'] = "0";
        $_SESSION['ems_amt'] = "0";
                 }
      
    $ix = count($_SESSION['ems_id']);
    $ix++;
     
    $_SESSION['ems_id'][5] = strval($rowEms['hs_id']);
    $_SESSION['ems_amt'][5] = strval($rowBuy2['hs_ems']);
    Its not working
    Session stores new data but not populates - Just replacing.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    In the IF block you are assigning scalar values to $_SESSION['ems_id'] and $_SESSION['ems_emt'], so those array elements are not arrays themselves, but you then try to assign array elements to them in the last two lines. I'm not real sure what you want to do, but I'm guessing this might be closer:
    PHP Code:
    if(isset($_SESSION['ems_id']) == NULL) {
        echo 
    "empty";
        
    $_SESSION['ems_id'] = array();
        
    $_SESSION['ems_amt'] = array()
    }
      
    $ix count($_SESSION['ems_id']);
    $ix++;
     
    $_SESSION['ems_id'][5] = strval($rowEms['hs_id']);
    $_SESSION['ems_amt'][5] = strval($rowBuy2['hs_ems']); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2009
    Posts
    23
    Quote Originally Posted by NogDog View Post
    In the IF block you are assigning scalar values to $_SESSION['ems_id'] and $_SESSION['ems_emt'], so those array elements are not arrays themselves, but you then try to assign array elements to them in the last two lines. I'm not real sure what you want to do, but I'm guessing this might be closer:
    PHP Code:
    if(isset($_SESSION['ems_id']) == NULL) {
        echo 
    "empty";
        
    $_SESSION['ems_id'] = array();
        
    $_SESSION['ems_amt'] = array()
    }
      
    $ix count($_SESSION['ems_id']);
    $ix++;
     
    $_SESSION['ems_id'][5] = strval($rowEms['hs_id']);
    $_SESSION['ems_amt'][5] = strval($rowBuy2['hs_ems']); 
    Great, I got your point and changed as

    if(isset($_SESSION['ems_id']) == NULL) {
    echo "empty";
    $_SESSION['ems_id'][0] = "0";
    $_SESSION['ems_amt'][0] = "0";
    }

    and it seems working.

    Thanks a lot

  4. #4
    Join Date
    Dec 2009
    Location
    Denmark
    Posts
    240
    PHP Code:
    $ix count($_SESSION['ems_id']);
    $ix++; 
    A small side remark.
    What to you wish to use the $ix for?
    If you are going to use it to assign values to the next index in the array, this would be wrong. Arrays starts at 0, but count will return the number of indexes including the first at 0.

    If you have an array with 2 indexes, they will be placed at 0 and 1. Count() will return 2, and your $ix will contain 4 instead of 3 which is the next index, because you assign $ix with +1.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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