130 users online (3 members and 127 guests)  

  Results 1 to 2 of 2


  1. mailto auto subject population variable    Forum: HTML Forum
    Replies: 0
  2. convert a perl variable to javascript variable    Forum: Javascript Forum
    Replies: 0
  3. Replies: 1
  4. Replies: 1
  5. Replies: 5
  1. #1
    lozza's Avatar
    New User

    Join Date
    Sep 2008

    auto increment session variable name?

    Hi all - hoping somebody can see this clearer than I can...

    Goal: to create a shopping cart page that receives $_POST and $_GET info and store it in a session variable so items will be displayed in cart on clients return to shopping cart page.

    Problem: I have no idea how to create a changing session variable. I can get all my data from the first product and store it in an array in the session variable (ex
    $_SESSION['01'] = array($id, $small, $medium, $large);

    which I can then output into a table. But how do I store the next product info in a DIFFERENT session variable without writing over the first?

    I have tried using a counter that changes the session variable each time a new product is added, ie:

    //session counter
    $counter = $_SESSION['counter'];
    if (($_GET['id'] !="") and ($value != "")){
    $counter = $counter + 1;
    $_SESSION['counter'] = array($id, $small, $medium, $large);

    The counter and session variable work, but since the actual variable is not "written" to the page, the data does not last.
    It is important the data stay in one string as there could be 1-6 sizes chosen with a quantity of 1-5 each, and I would need to know what sizes and quantities go with what product id.

    Whew - let me know if I am being concise enough, or if I am barking up the wrong tree.

    Thanks a bunch!

  2. #2
    jthayne's Avatar

    Join Date
    Aug 2008

    Re: auto increment session variable name?

    You are on the right track, but I would go a slightly different direction. Try the following instead:

    PHP Code:
    //session counter
    $counter count($_SESSION['cart_items']);
    if ((
    $_GET['id'] !="") and ($value != "")){
    $counter $counter 1;
    $_SESSION['cart_items'][$counter] = array($id$small$medium$large);
    A couple recommendations for you:

    1. Don't use method=get to pass shopping cart variables. Use method=post instead unless you have to pass a variable using a link.
    2. Make sure you verify the data that comes across from the form so that you don't inadvertently get data that will cause a security breach.

    Let me know if there is anything else I can do to help or if you have any questions about the code I gave you.

Tags for this Thread