SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    London
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multiple values held by a session variable?

    Hi here’s some code I wrote earlier

    [code]

    //page1.php
    <a href=”page2.php?id=1”>Item 1</a><br />
    <a href=”page2.php?id=2”>Item 2</a><br />
    <a href=”page2.php?id=3”>Item 3</a><br />
    <a href=”page2.php?id=4”>Item 4</a><br />
    <a href=”page2.php?id=5”>Item 5</a><br />


    //page2.php

    <?php
    session_start();

    if(isset($_GET[‘id’])
    {
    $_SESSION[‘shopping_cart’] = $_GET[‘id’];
    }

    ?>

    Simply what I want is a way for $_SESSION[‘shopping_cart’] to be able to hold all the id’s for each respective item selected. For example if the user selected item 1, 3 and 5 the value for $_SESSION[‘shopping_cart’] should be 1, 3 and 5. How could I do that? Can the session become an array and I could loop through it with a foreach loop?

    [/code/

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by juanc
    Can the session become an array and I could loop through it with a foreach loop?
    Yes. $_SESSION can contain any variable, except resource.

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    London
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks but I was hoping someone could give me the code for how it would done. Only because what I've tried in the past hasn't worked.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Untested, but I think this would work :

    //shoppingcart.php
    PHP Code:
    <?php
    class ShoppingCart
    {
        protected 
    $products = Array();

        function 
    getInstance() {
            if (
    session_id() == "") {
                
    session_start();
            }
            if (!isset(
    $_SESSION['ShoppingCart'])) {
                
    $_SESSION['ShoppingCart'] = new ShoppingCart();
            }
        }

        function 
    addProduct($id) {
            if (!isset(
    $this->products[$id])) {
                
    $this->products[$id] = 0;
            }
            
    $this->products[$id]++;
        }

        function 
    getProducts() {
            return 
    $this->products;
        }
    }
    ?>
    //page2.php
    PHP Code:
    <?php
    require_once 'shoppingcart.php';
    if (isset(
    $_GET['id']) {
        
    $cart ShoppingCart::getInstance();
        
    $cart->addProduct($_GET['id']);
    }

    ?>

  5. #5
    SitePoint Zealot euringroy's Avatar
    Join Date
    Sep 2005
    Location
    Staffordshire, England
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The syntax you need for this is:
    PHP Code:
      $_SESSION['shopping_cart'][$id] = $value
    Although I am not sure of the program logic of what you are trying to achieve, I imagine that setting the value to the quantity required would be the most useful approach.

    Subsequently, you can use a foreach on $_SESSION['shopping_cart']
    Eric "Motorlegs" Roy
    If you can't keep up - stay at home

  6. #6
    SitePoint Member
    Join Date
    Aug 2006
    Location
    Sighisoara/ClujNapoca - Romania
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    This might help.

    PHP Code:
    <?php
     session_start
    ();
     
     if (isset (
    $_GET['id']))
     {
        
    $_SESSION['shopping_cart'][$_GET['id']] = $_GET['id'];
     }
     
    ?>
    Every time you get id passed via URL the $_SESSION['shopping_cart'] array increases having the id as new key and it's value.

    Example:
    Code:
    [shopping_cart] => Array
             (
                 [2] => 2
                 [4] => 4
                 [8] => 8
                 [0] => 0
                 [11] => 11
             )
    Hope this is what you're looking for.

    Boby

  7. #7
    Non-Member Ihtesham's Avatar
    Join Date
    Aug 2006
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Extending Frozenminds' example:

    PHP Code:
    <?php
    session_start
    ();

    if (isset (
    $_GET['id']))
    {
        
    // Having ~ item_id => required_quantity ~ can be more useful
        
    $_SESSION['shopping_cart'][$_GET['id']] = $_GET['quantity'];
    }
    ?>


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
  •