Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Removing a $_SESSION entity if values = X

    [s]Is there a way to make it[/s] How can I go about removing a $_SESSION entity if certain array values of it equal a specific value? I am thinking something along the lines of this:

    PHP Code:
    if (isset($_SESSION['cart']['content'][$_POST['id']])) = array ('sizes' => 0'sizem' => 0'sizel' => 0'sizexl' => 0) {
        
    // remove $_SESSION['cart']['content'][$_POST['id']]


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Isset cannot be used to check against a value. You'll need to check each value after checking to see if it exists.
    If this were OO code, it would be far less work as you could pass the instruction through the 'cart' to each child recursively to determine if it should remove itself. Collections have a huge advantage over arrays when it comes to simplifying what they can do.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You'll need to check each value after checking to see if it exists.
    I'm not quite sure what your saying, could you give me an example?

    Quote Originally Posted by Fou-Lu View Post
    If this were OO code, it would be far less work as you could pass the instruction through the 'cart' to each child recursively to determine if it should remove itself.
    Sounds good. Whats OO code?

    Quote Originally Posted by Fou-Lu View Post
    Collections have a huge advantage over arrays when it comes to simplifying what they can do.
    What is a collection?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by FlashDance View Post
    I'm not quite sure what your saying, could you give me an example?
    PHP Code:
    if (isset($_SESSION['cart']['content'][$_POST['id']]))
    {
        list(
    $sizes$sizem$sizexl) = $_SESSION['cart']['content'][$_POST['id']];
        if (
    $sizes == && $sizem == && $sizexl == 0)
        {
            unset(
    $_SESSION['cart']['content'][$_POST['id']]);
        }

    Is probably the easiest way. Many approaches to determining if an array is empty, but with associative indexes that appear to always be defined you cannot check on empty.

    Sounds good. Whats OO code?
    Object oriented.

    What is a collection?
    A collection is exactly as it sounds - a collection of a datatype. There are dozens of collections that can be written. PHP's core only has one: Array and that is simply because PHP doesn't have actual arrays they only have Hashtables that they call arrays.

  • Users who have thanked Fou-Lu for this post:

    FlashDance (10-13-2011)

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP Code:
    if (isset($_SESSION['cart']['content'][$_POST['id']]))
    {
        list(
    $sizes$sizem$sizexl) = $_SESSION['cart']['content'][$_POST['id']];
        if (
    $sizes == && $sizem == && $sizexl == 0)
        {
            unset(
    $_SESSION['cart']['content'][$_POST['id']]);
        }

    Is probably the easiest way. Many approaches to determining if an array is empty, but with associative indexes that appear to always be defined you cannot check on empty.
    The flow you suggested is very good. Its the style I program, so thanks for introducing unset to me.
    I wish the PHP documentation included common ways how to do things, instead of people having to ask someone.

    Thanks again man.

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    I discovered a problem with the code with what I need it for.

    If $sizes or $sizem or $sizexl equal 0, then it removes the entire $_SESSION entity.
    So if, $sizes = 0 and $sizem = 1 and $sizexl = 3, then it will delete the entire entity from the session because $sizes = 0.
    I was looking for so it will delete the $_SESSION entity if only all the 'array attributes' equal 0.

    I tried this but it didn't work:

    PHP Code:
    if (isset($_SESSION['cart']['content'][$_POST['id']]))
    {
        list(
    $sizes$sizem$sizexl) = $_SESSION['cart']['content'][$_POST['id']];
        if (
    $sizes == $sizem == $sizexl == 0)
        {
            unset(
    $_SESSION['cart']['content'][$_POST['id']]);
        }

    I also tried:
    PHP Code:
    if ($sizes == || $sizem == || $sizexl == 0
    PHP Code:
    if ($sizes || $sizem || $sizexl == 0
    PHP Code:
    if ($sizes && $sizem && $sizexl == 0
    .. but no success.

    I've actually got a solution (EDIT: it doesn't work, it just deletes everything!!!!!), but its a long and probably bad practice that would get me some funny stares lol, so wondering if anyone has a suggestion?

    PHP Code:
    if ($sizes == 0) {
        if (
    $sizem == 0) {
            if (
    $sizel == 0) {
                
    // do delete;
            
    }
        }

    Last edited by FlashDance; 10-13-2011 at 03:22 AM.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    double post.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    & != &&
    & is a bitwise operator. Essentially, you have done:
    Code:
    true || false & true || false & true || false
    The end results of this is true if any one of those is not equal to 0.

    If your intent is to remove the entire ['cart']['content'][$x] array only if all the conditions are 0, you have to use &&. You can do each offset alone by conditionally checking each and only unsetting the one value.

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    If your intent is to remove the entire ['cart']['content'][$x] array only if all the conditions are 0, you have to use &&. You can do each offset alone by conditionally checking each and only unsetting the one value.
    I'm very confused by this. The above is exactly what I see happening here:
    PHP Code:
    if ($sizes == && $sizem == && $sizel == && $sizexl == 0
    Could you give me an example? I understand allot better visually.

  • #10
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Solved:

    PHP Code:
    if ($_SESSION['cart']['content'][$_POST['id']]['sizes'] == 0) {goto sizem;} else {goto done;}
    sizem:
    if (
    $_SESSION['cart']['content'][$_POST['id']]['sizem'] == 0) {goto sizel;} else {goto done;}
    sizel:
    if (
    $_SESSION['cart']['content'][$_POST['id']]['sizel'] == 0) {goto sizexl;} else {goto done;}
    sizexl:
    if (
    $_SESSION['cart']['content'][$_POST['id']]['sizexl'] == 0) {unset($_SESSION['cart']['content'][$_POST['id']]);}
    done


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •