Question abut Sessions!

hi! What does it mean when you reference a separate non-session variable right next to a Session Variable in brackets?

Like

 if(isset($_SESSION['cart']['new]'])){
        $_SESSION['cart'][$new]++;
    } else{
        $_SESSION['cart'][$new]=1;

or

if(isset($_POST['save'])) {
    foreach($_SESSION['cart'] as $isbn=> $qty) {
        if($_POST[$isbn] == '0') {
            unset($_SESSION['cart'][$isbn]);
        } else{
            $_SESSION['cart'][$isbn]=$_POST[$isbn];
        }

or

  if(isset($_SESSION['cart']['new]'])){
        $_SESSION['cart'][$new]++;
    } else{
        $_SESSION['cart'][$new]=1;
    }

Here’s the example code where it came from:


<?php
/**
 * Created by PhpStorm.
 * User: John
 * Date: 3/21/14
 * Time: 11:45 AM
 * show_cart.php
 */

<?$php_errormsg
    include('book_sc_fns.php');
//The shopping cart needs sessions, so start one
session_start();
@$new=$_GET['new'];

if($new) {
    //new item selected
    if(!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = array();
        $_SESSION['items']= 0;
        $_SESSION['total price'] ='0.00';
    }
    if(isset($_SESSION['cart']['new]'])){
        $_SESSION['cart'][$new]++;
    } else{
        $_SESSION['cart'][$new]=1;
    }
    $_SESSION['total_price']= calculate_price($_SESSION['cart']);
    $_SESSION['items']= calculate_items($_SESSION['cart']);
}

if(isset($_POST['save'])) {
    foreach($_SESSION['cart'] as $isbn=> $qty) {
        if($_POST[$isbn] == '0') {
            unset($_SESSION['cart'][$isbn]);
        } else{
            $_SESSION['cart'][$isbn]=$_POST[$isbn];
        }
    }
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items'] = calculate_items($_SESSION['cart']);
}
do_html_header("Your shopping cart");

if(($_SESSION['cart'])&& (array_count_values($_SESSION['cart']))){
    display_cart($_SESSION['cart']);
} else {
    echo"<p>There are no items in your cart</p></hr>";
}
$target = "index.php";

//if we have just added an item to the cart, continue shopping in that category
if($new) {
    $details= get_book_details($new);
    if($details['catid']) {
        $target = "show_cat.php?catid =".$details['catid'];
    }
}
display_button($target, "continue-shopping", "Continue Shopping");

//use this if SSL is set up
//path = $_SERVER['PHP_SELF'};
//$server = $-SERVER['SERVER_NAME'];
//path = str_replace('show_cart.pjp', '',$path;
//display_button(https://".server.$path."checkout.php", "go-to-checkout", "Go To Checkout");

//if no SSL use below code
display_button("checkout.php", go-to-checkout", "Go to Checkout");
do_html_footer();
?>
")

Hi John,

$_SESSION is an associative array (like the other superglobal variables), and in PHP you can nest arrays within arrays (ie. to create multidimensional arrays), which is basically what you’ve got here.

When you see something like $_SESSION['cart']['new'] is you’re actually referencing an element of the $_SESSION array (‘cart’) which holds another array, and you’re then referencing an element with the key ‘new’ in that sub-array.

The actual structure would look similar to this if you called print_r($_SESSION):


Array (
  'cart' => Array (
        'new' => 'some value'
    )
)

So using GET new=item_name adds the “item_name” to the array as a key like so.

$_SESSION['cart']['item_name']

The variables are looking for the key (based on that GET) in the array, however this line makes no sense to me

if(isset($_SESSION['cart']['new]'])){

Why look for [‘new’]?

I would think you’d want to look for the item_name or array KEY and if the item isset() , up the quantity by one.

if(isset($_SESSION['cart'][$new])){
$_SESSION['cart'][$new]++;
} else{
$_SESSION['cart'][$new]=1;
}

Just me two cents

OHHH, fretburner does it again. Hee he.

Thank you for answering fretburner and Drummin! I’m going to check it and go over it when I get home.