SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need advice with sessions

    Hi,
    I have a page where people (known as agents) can order stuff on behalf of other for other customers.(could be many other customers, not just one)

    It works like this:

    the user enter their details
    go to next page,
    the user enters their first customer details
    go to order page
    the user enter what they want to order and sends order
    I am using sessions to keep details stored until the final email page

    At the moment, each order made by the agent on behalf of a customer comes through in a separate email. What I want is to be able to store multiple customer details and orders per agent, so that everything can be sent in one email when the agent has finished ordering

    My problem is that after the agent orders something, the email gets sent, because I dont know how to keep storing more and more things in the session. The agent then goes back to the customer detail page, adds the next customer details, and then order, and then another order is sent.

    Basically what I want to know if how can I keep adding new customer details and orders to a session?

    At the moment I have stically named session variables. Is there a way to make these act as an arrray maybe, so that for example, a new customers firstnames can keep be added to a session variable of one names, and then pulled out one by one,

    thanks a lot for any help

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have PHP > 4.1.3 then you can handle session variables very easily with the superglobal $_SESSION array.

    So you can do things like
    PHP Code:
    $_SESSION['orders'][] = array('Mr. Test''foo'42); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Hi

    Quote Originally Posted by Gaheris
    If you have PHP > 4.1.3 then you can handle session variables very easily with the superglobal $_SESSION array.

    So you can do things like
    PHP Code:
    $_SESSION['orders'][] = array('Mr. Test''foo'42); 
    Hi,
    thanks,

    so for example, I would keep adding customers firstnames to
    $_SESSION['custFirstName][] = array('first, 'first2', 'first3');

    and second names to

    $_SESSION['custSecondName][] = array('second', 'second2', second3);

    but how is this done practically? say if I have a field with the cust name in it woudl I just do:

    $_SESSION['custSecondName][] = $_POST['textfield'];
    and then after the user has gone round a second time just do :

    $_SESSION['custSecondName][] = $_POST['textfield2'];

    and this would mean two names are stored in that one session variable?
    thanks

    I just found I have version 4.1.2, is this still available?

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could just use:
    PHP Code:
    $_SESSION['orders']['custLastName']['firstName'] = $firstname;
    $_SESSION['orders']['custLastName']['textfield'] = $_POST['textfield'];
    $_SESSION['orders']['custLastName']['textfield2']= $_POST['textfield2']; 
    so all of the orders would be in $_SESSION['orders'] and all customers by last name are found at $_SESSION['orders']['custLastName'] with their details in an associative array under their last name.
    Erh

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was just an example, the $_SESSION array can be used like any other array to store data in it.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    am new at this,
    so if I do this when 'textfield has a value of Smith.
    $_SESSION['custSecondName][] = $_POST['textfield'];

    and then a few page laters do this, when t'textfield' has a value of Hill
    $_SESSION['custSecondName][] = $_POST['textfield'];

    Will both Smith and Hill be stored in $_SESSION['custSecondName][]? and if so how then do I echo them out?
    thanks a lot

  7. #7
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well it would be easier IMO to do this:
    PHP Code:
    $_SESSION['custSecondName']['textfield'] = $_POST['textfield']; 
    Then you can refer to the value as an associative array. Now this is assuming that your not going to duplicate field names from page to page. If you do that then of course you wouldnt want to name them like above but if I may suggest that you dont duplicate field names even if they are on different pages.

    Anyway from here you could echo them all out in the simplest case as:
    PHP Code:
    foreach($_SESSION as $custLastName=>$data){
      echo 
    $custLastName;
      foreach(
    $data as $fieldName=>$value){
        echo 
    $fieldName."=".$value;
      }

    Of course exactly what the structure of the array is, is dependent on the context you will use it in and the context of your forms.
    Erh

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    London
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI,
    thanks,
    actually I am duplicating names, because what happens is, after the user ahs placed an order on behalf of one customer they have to go back to the same add customer page and type in a new set of details and then add an order for them, and this should be able to continue indefinately,
    is there a way around this?
    thanks

  9. #9
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so the structure would look like this?:

    orders->user1->customer1->orderdetails
    orders->user1->customer2->orderdetails
    orders->user1->customer3->orderdetails
    orders->user2->customer4->orderdetails
    Erh


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
  •