#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2

    Unsetting and updating sessions


    I am having trouble, the code to add and remove items from a cart works ok. The only problem is the variable $key1. That variable controls the count for items in the basket. My code unssets sessions based on what is submitted from a form. However, the resulting delete operation rebuilds my list with $key1 as the original place instead of resetting from 1.)

    I.E...
    Item1 - Delete? YES
    Item2 - Delete?

    Resulting list:
    item 2 - Delete? (This should be item 1)

    Here is my code for the header that is included:
    PHP Code:
    session_start();

    if (empty(
    $_SESSION['count_cart'])) {
        
    $_SESSION['count_cart'] = 0;
    }

    // Handle Delete Item //
    if ($_POST['submitted']) {
        
    $delItem $_POST['delItem'];
        unset(
    $_SESSION['item'][$delItem][0]);
        unset(
    $_SESSION['item'][$delItem][1]);
        unset(
    $_SESSION['item'][$delItem][2]);
        unset(
    $_SESSION['item'][$delItem][3]);
        
    $_SESSION['count_cart']--;
        unset(
    $_POST['submitted']);
    }

    // Handle setting sessions //
    if ($_POST['name'] == $PHPSESSID && isset($_POST['mdl_key'])) {
        
    $_SESSION['count_cart']++;
        
    $idx $_SESSION['count_cart'];
        
    $_SESSION['item'][$idx][0] = $_POST['mdl_key'];
        
    $_SESSION['item'][$idx][1] = $_POST['model'];
        
    $_SESSION['item'][$idx][2] = $_POST['manufacturer'];
        
    $_SESSION['item'][$idx][3] = $_POST['sellprice'];
        
    $_SESSION['uri'][$idx] = $_POST['uri'];

    And here is the code for the shop cart page:
    PHP Code:
    <form name="del" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
    foreach ($_SESSION['item'] as $key1=>$v1) { 
        if (isset(
    $_SESSION['item'][$key1][0])) {
        echo 
    "<tr><td width='30px'></td><td bgcolor='white'>".$key1.".) ";
        foreach (
    $v1 as $v2){
            echo 
    $v2." ";
        }
    echo 
    " - <a href='".$_SESSION['uri'][$key1]."' target='_blank'>View Details</a> | 
    <input type='submit' value='Delete' name='delete'/>
    <input type='hidden' name='delItem' value='"
    .$key1."'/>
    <input type='hidden' name='submitted' value='TRUE'/>
    </td><td width='80px'></td></tr>"
    ;
        }
    }
    ?>
    </form>
    Last edited by BitZoid; March 23rd, 2013 at 12:26 PM.
    -- Success achieved from tribulation --
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,466
    Rep Power
    594
    You can unset a specific element but that will not automatically reorder the array with the remaining items. You will need to do that yourself on a delete.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    In addition to what gw1500se said, $_SESSION['item'] is still going to contain two elements. You unset all of the elements of $_SESSION['item'][X] but you never unset $_SESSION['item'][X] itself; therefore $_SESSION['item'] will still contain it.

    An easy way to reindex an array is to use array_values on it.

    However, a better alternative in this case might be to just count the items when you display them rather than when you store them, since the order they're stored in doesn't seem to matter.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    I took your advice, and I tried gw's way and used array_values and couldn't get that to work, so I switched the item count to generate when displaying, and it almost works however I'm getting some odd results. The first item never disappears when it's the only one, and it doesn't always delete the correct item. Here my new code with changes:

    header included file:
    PHP Code:
    session_start();

    // Help Debug -- REMOVE WHEN DONE //
    if ($_GET['des']) {
        
    session_destroy();
        unset(
    $_GET['des']);
    }

    // ---- //

    // Count Cart Items //
    if (empty($_SESSION['count_cart'])) {
        
    $_SESSION['count_cart'] = 0;
    }

    // Handle Delete Item //
    if ($_POST['submitted']) {
        
    $delItem $_POST['delItem'];
        
    $_SESSION['count_cart']--;
        unset(
    $_SESSION['item'][$delItem]);
        unset(
    $_POST['submitted']);
    }

    // Handle setting sessions //
    if ($_POST['name'] == $PHPSESSID && isset($_POST['mdl_key'])) {
        
    $_SESSION['count_cart']++;
        
    $idx $_SESSION['count_cart'];
        
    $_SESSION['item'][$idx][0] = $_POST['mdl_key'];
        
    $_SESSION['item'][$idx][1] = $_POST['model'];
        
    $_SESSION['item'][$idx][2] = $_POST['manufacturer'];
        
    $_SESSION['item'][$idx][3] = $_POST['sellprice'];
        
    $_SESSION['uri'][$idx] = $_POST['uri'];

    shopping cart:
    PHP Code:
    <form name="del" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <?php
    $i
    =0;
    foreach (
    $_SESSION['item'] as $key=>$v1) { 
        if (isset(
    $_SESSION['item'][$key])) {
            echo 
    "<tr><td width='30px'></td><td bgcolor='white'>".($i+1).".) ";
            foreach (
    $v1 as $v2){
                echo 
    $v2." ";
            }
        }
    echo 
    " - <a href='".$_SESSION['uri'][$i]."' target='_blank'>View Details</a> | 
    <input type='submit' value='Delete' name='delete'/>
    <input type='hidden' name='delItem' value='"
    .$i."'/>
    <input type='hidden' name='submitted' value='TRUE'/>
    </td><td width='80px'></td></tr>"
    ;
    $i++;
    }
    ?>
    </form>
    -- Success achieved from tribulation --
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    This is really getting to me, please help me with code. I tried array_values and ksort but am too unfamiliar with php arrays and syntax.
    -- Success achieved from tribulation --
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    Here's a new idea, go back to numbering the items like you did before but when displaying them set them to new numbers and handle these new numbers accordingly

    item1 = 1
    item2 = 2

    user deletes 1
    1 = deleted so 1 -= 1 making item1 = 0 so item1 = deleted

    then 2 -= 1
    item1 = deleted
    item2 = 1

    so let's say item1 is a pair of shoes and item2 is a pair of socks

    they are shown in that order so item1 is 1 and item2 is 2

    The user decided against the shoes and cancels the order

    item1 which is the shoes would become deleted and item2 would become 1 because it is the only (selection/order) left

    This works the same way even if there is 4 items selected, the seconded item gets deleted so the third and fourth items both get their display numbers reduced by 1 making the third item display as the second(2) and the fourth display as the third(3)
    while the second item that got deleted would have a display of 0


    just my thoughts on this problem
    Last edited by jack13580; March 24th, 2013 at 04:33 AM.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    however I'm getting some odd results. The first item never disappears when it's the only one, and it doesn't always delete the correct item.
    The problem is actually still the same - PHP doesn't reindex arrays when you remove an item from it.

    For example, if your $_SESSION['item'] array contains the keys:
    1
    2
    3

    And you delete 2, then you have
    1
    3

    This causes two problems for you:

    First, in your delete form, you set 'delItem' equal to $i, which is the position of the element in the array - not the index of the element in the array. The index is $key.

    The second issue is the way you're using $_SESSION['count_cart']. Personally, I would just drop that variable completely. If you need to determine the number of items that are in the cart, use count($_SESSION['item']) instead.

    The problem is with this section of code:
    PHP Code:
        $_SESSION['count_cart']++;
        
    $idx $_SESSION['count_cart'];
        
    $_SESSION['item'][$idx][0] = $_POST['mdl_key'];
        
    $_SESSION['item'][$idx][1] = $_POST['model'];
        
    $_SESSION['item'][$idx][2] = $_POST['manufacturer'];
        
    $_SESSION['item'][$idx][3] = $_POST['sellprice'];
        
    $_SESSION['uri'][$idx] = $_POST['uri']; 
    In the scenario I gave at the beginning of the post, your array contains keys 1 and 3, and 'count_cart' is 2. If you add a new item to the cart using that block of code, you are going to overwrite the product stored at index 3. The same thing would happen if you used count($_SESSION['item']) to determine the index instead.

    What you're actually trying to do is just add a new element to the array. You don't actually care what its index is. There are a variety of ways of doing this, the most explicit way is to use the array_push function.
    PHP Code:
        array_push($_SESSION['item'], array($_POST['mdl_key'], $_POST['model'], $_POST['manufacturer'], $_POST['sellprice'])); 
    If you do care about its index is, you can determine the next-highest index using something like: max(array_keys($array))+1. There is no way to do it based on the length of the array.

    I don't quite understand why you're tracking 'uri' in a separate array. Your delete function doesn't remove it from the $_SESSION['uri'] array, so that's going to make the two arrays get out of sync. It would be better to store it with the rest of the data for that product.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    What I mean is that the variables in the array are only for holding the name of the item and nothing more so their order doesn't matter but the order in which they are displayed does matter

    When the item is deleted all you have to do is change the name of that item in the array to deleted so it is ignored in the processing part of the store

    All you have to do is assign numbers to the variables in the array and display them based on those assigned numbers

    you can do this with a simple for loop and when an item is deleted just subtract the numbers that are bigger then that number that was deleted by 1 so they fall in place
    Last edited by jack13580; March 24th, 2013 at 04:24 PM.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    I redesigned my array and used array_push (similar to push_back with vectors in C++). It works and deletes in a manner that is correct. However, the count($_SESSION['item']) always begins with returning 1 instead of 0. I'm confused, heres my code:

    Shopping cart:
    PHP Code:
    <table width="55%" bgcolor="white">
        <tr><td bgcolor="#d0d0d0" colspan="3"><h3>Items:</h3></td></tr>
        <?php
        $i
    =1;
        foreach (
    $_SESSION['item'] as $key=>$v1) {
            foreach(
    $v1 as $v2) {
                echo 
    "<tr><td>".$i.") ".$v2."</td></tr>";
            }
        echo 
    "<form name='del' method='post' action='".$_SERVER['PHP_SELF']."'>
         - <a href='"
    .$_SESSION['item'][$key]."' target='_blank'>View Details</a> | 
        <input type='submit' value='Delete' name='delete'/>
        <input type='hidden' name='delItem' value='"
    .$key."'/>
        <input type='hidden' name='delete' value='TRUE'/>
        </td><td width='80px'></td></tr>
        </form>"
    ;
        
    $i++;
        }
        
    ?>
    </table>
    header file included in shop cart and all other pages:
    PHP Code:
    session_start();

    // Handle Delete Item //
    if ($_POST['delete'] == TRUE) {
        
    $delItem $_POST['delItem'];
        unset(
    $_SESSION['item'][$delItem]);
        unset(
    $_POST['delete']);
    }

    if (
    count($_SESSION['item']) == 0) {
        
    $idx count($_SESSION['item']);
    }

    // Set session for items
    if (!isset($_SESSION['item'])) {
        
    $item = array
        (
            
    "itemID"=>array
            (
    $_POST['mdl_key'])
        );
        
    $_SESSION['item'] = $item;
    }

    // Handle setting sessions //
    if ($_POST['name'] == $PHPSESSID && $_POST['add'] == TRUE) {
        
    array_push($_SESSION['item'], array($_POST['mdl_key']));

    -- Success achieved from tribulation --
  18. #10
  19. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    However, the count($_SESSION['item']) always begins with returning 1 instead of 0.
    When you initialize the array you are adding an element to it immediately:
    PHP Code:
    // Set session for items
    if (!isset($_SESSION['item'])) {
        
    $item = array
        (
            
    "itemID"=>array
            (
    $_POST['mdl_key'])
        );
        
    $_SESSION['item'] = $item;

    Your initialization code there differs from the code you use when adding an item though, so I think there is probably some error there. When you add an item you don't use the itemID element of the item array to store it. If your goal is to store the item IDs as $_SESSION['item']['itemID'][0], $_SESSION['item']['itemID'][1], $_SESSION['item']['itemID'][2], etc. then to count the items you have to call count($_SESSION['item']['itemID']), because $_SESSION['item'] will only ever contain one element (at index 'itemID').
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    98
    Rep Power
    2
    I didn't understand that when using associative arrays the element is accessed by name, not number. I was using the a number instead of a name to access those elements. It works now, thank you!
    -- Success achieved from tribulation --
  22. #12
  23. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    New York
    Posts
    28
    Rep Power
    0
    Can you specify the element which is not automatically reorder the array with the remaining items.By the way you can easily do it by yourself by editing or deleting it.

IMN logo majestic logo threadwatch logo seochat tools logo