Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2011, 02:49 PM   PM User | #1
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
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']]

FlashDance is offline   Reply With Quote
Old 10-12-2011, 02:56 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 10-12-2011, 03:13 PM   PM User | #3
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
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?
FlashDance is offline   Reply With Quote
Old 10-12-2011, 03:46 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.

Quote:
Sounds good. Whats OO code?
Object oriented.

Quote:
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
FlashDance (10-13-2011)
Old 10-13-2011, 01:21 AM   PM User | #5
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
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.
FlashDance is offline   Reply With Quote
Old 10-13-2011, 02:55 AM   PM User | #6
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
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..
FlashDance is offline   Reply With Quote
Old 10-13-2011, 03:01 AM   PM User | #7
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
double post.
FlashDance is offline   Reply With Quote
Old 10-13-2011, 03:21 AM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
& != &&
& 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.
Fou-Lu is offline   Reply With Quote
Old 10-13-2011, 03:28 AM   PM User | #9
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
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.
FlashDance is offline   Reply With Quote
Old 10-13-2011, 04:28 AM   PM User | #10
FlashDance
Regular Coder

 
Join Date: Sep 2011
Posts: 274
Thanks: 38
Thanked 0 Times in 0 Posts
FlashDance can only hope to improve
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
FlashDance is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:13 AM.


Advertisement
Log in to turn off these ads.