SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why does this fail?

    Hi,

    I have a basket page made up of the contents of a session, If I have 3 products in by basket and dump the session array to screen, this is what I see.

    array (

    0 => class stdClass { public $brand_name = 'Armani City Glam'; public $brand_id = '17'; public $price = '32.99'; public $product_name = 'Armani City Glam 100ml EDT'; public $bid = '17'; public $pid = '10'; public $mid = '11'; public $size = ''; public $manufacturer_name = 'Armani'; public $image = '/images/product/picture10.jpg'; public $stock = '100'; public $quantity = 1; },

    1 => class stdClass { public $brand_name = 'Armani City Glam'; public $brand_id = '17'; public $price = '29.99'; public $product_name = 'Armani City Glam'; public $bid = '17'; public $pid = '7'; public $mid = '11'; public $size = '50ml EDT'; public $manufacturer_name = 'Armani'; public $image = '/images/product/picture7.jpg'; public $stock = '4'; public $quantity = 1; },

    2 => class stdClass { public $brand_name = 'Joop Homme'; public $brand_id = '15'; public $price = '12.22'; public $product_name = 'spray'; public $bid = '15'; public $pid = '9'; public $mid = '10'; public $size = '12'; public $manufacturer_name = 'Joop!'; public $image = '/images/product/picture9.jpg'; public $stock = '10'; public $quantity = 1; },

    )

    The basket works great as I just loop over the items and print to screen, however, when I come to allowing customers to delete items from the basket something strange happens.

    I use this code to delete an item from the session array:

    PHP Code:
    if (isset($_GET['id'])) { 
     unset(
    $_SESSION['basket'][$_GET['id']]); 
     
    header ("Location: /basket/");

    This is the anchor to achieve the delete:

    PHP Code:
    <a href="/basket/?id={$smarty.section.item.index}">Remove Item(s)</a
    If the visitor clicks the delete button for the 2nd or 3rd product in the list (on screen) the delete function works perfectly, the item is removed from the session array.

    However, as soon as the delete link for the first item in the session is clicked, the whole session gets messed up. Clicking the first delete link produces:

    array (

    1 => class stdClass { public $brand_name = 'Armani City Glam'; public $brand_id = '17'; public $price = '29.99'; public $product_name = 'Armani City Glam'; public $bid = '17'; public $pid = '7'; public $mid = '11'; public $size = '50ml EDT'; public $manufacturer_name = 'Armani'; public $image = '/images/product/picture7.jpg'; public $stock = '4'; public $quantity = 1; },

    2 => class stdClass { public $brand_name = 'Joop Homme'; public $brand_id = '15'; public $price = '12.22'; public $product_name = 'spray'; public $bid = '15'; public $pid = '9'; public $mid = '10'; public $size = '12'; public $manufacturer_name = 'Joop!'; public $image = '/images/product/picture9.jpg'; public $stock = '10'; public $quantity = 1; },

    3 => class stdClass { public $brand_name = 'Joop Homme'; public $brand_id = '15'; public $price = '12.22'; public $product_name = 'spray'; public $bid = '15'; public $pid = '9'; public $mid = '10'; public $size = '12'; public $manufacturer_name = 'Joop!'; public $image = '/images/product/picture9.jpg'; public $stock = '10'; public $quantity = 1; },

    )

    The key 0 (the first item is deleted) thus leaving 1, 2 and in this case 3 available, sorry their were 4 products in this example. Does removing the 0 key cause problems? Is this what could be causing me problems?

    If so , what is the best way of removing an item from a session array?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you never told us what this means
    the whole session gets messed up
    why do you consider this messed up? what behavior did you expect/desire? what behavior is it exibiting?

    i have a feeling though, that your problem might be solved by reindexing your array after you unset an index from it.


    this will make your array start at index 0 again.


    try
    PHP Code:
    if (isset($_GET['id'])) {
    unset(
    $_SESSION['basket'][$_GET['id']]);
    $_SESSION['basket'] = array_values($_SESSION['basket']);
    header ("Location: /basket/");


  3. #3
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I expected to happen was the product would be removed from the session array. It only seems to work with any array key over 0, when I try it with 0 the sessions seems to go hay wire. I will give this a try.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I expected to happen was the product would be removed from the session array. It only seems to work with any array key over 0, when I try it with 0 the sessions seems to go hay wire. I will give this a try.

    What does your bit of code do?

  5. #5
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have just tried the code you posted and it seems to work fine. So what could have been the problem?

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i dont know, you never told us what went wrong. you just used descriptions like haywire and messed up, that doesnt tell us anything at all.

    but its likely has something to do with how you loop through the array.


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
  •