Hi,

Can someone clear this up for me.

I want to use an array in a session variable so I do:

$_SESSION['cart[]']; to initialize. Or is it better to do:

$_SESSION['cart'] = array();

I receive a value (new) from another page so I do:

if($GET['new']
$new = $
GET['new'];

I want to now check and see if this value exists in my variable $cart so I try:

if(array_key_exists("$new", $cart))
$cart("$new")++;

//(not sure if this is correct but if the key $new already exists in the array, I want to add 1 to the value matching the key $new)

else // it does not exist in the array so

$cart("$new") = 1;

// assign a value of 1 to the key $new

Does this look right? Because this is a session variable, do I have to use $_SESSION somehow with $cart in order to manipulate this array or is it sufficient once the variable has been registered to use only $cart? NOTE: I have register_globals turned off.

Thanks in advance.

Dave

    $_SESSION['cart[]'] I believe will create a variable cart[], so use the second method.

    To use $GET['new'], you can simply use $GET['new'] as the variable or something like this:

    <?php
    isset($_GET['new'])
        ? $new = $_GET['new']   // Sets $new to $_GET['new'] if it is set
        : '';                   // Does nothing, $_GET['new'] is not set
    ?>

    To check if a key exists, you can use either or these methods:

    <?php
    array_key_exists('new', $cart)
        ? print('new exists')            // $new is set
        : print('new does not exist');   // $new is not set
    ?>

    or

    <?php
    isset($cart['new'])
        ? print('new exists')            // $new is set
        : print('new does not exist');   // $new is not set
    ?>

      OK .. I really am stumped here. Looking at your examples, why doesn't the following code work?

      <?php 
      session_start();
      include('assets/includes/pc01_store_fns.php'); ?>
      <?php
      
      
      //do_html_header('Your Shopping Cart');
        if(isset($_GET['new']))
        {
        $new=$_GET['new'];
        echo "<font color=\"#FFFFFF\">top of the script</font><br>";
        echo "<font color=\"#FFFFFF\">the itemSKU is $new</font><br>";
          //new item selected
          if(!$_SESSION["cart"])
          {
      	  //session_register("cart");
      	  //$cart = array();
      	  //$_SESSION['cart'] = array();
      	  $_SESSION['cart'] = array();
      	  if(is_array($_SESSION['cart']))
      	  echo "<font color=\"#FFFFFF\" \>Cart is an array<br>";
      	  $_SESSION["items"] = 0;  
      $_SESSION["total_price"] = "0.00"; //echo "total price is $_SESSION['total_price']"; $sessionId = session_id(); } print_r($_SESSION); if(array_key_exists($_GET['new'], $cart) { //echo "<br>The items is already in the cart. Up the quantity by 1"; $cart($_GET['new')++; }else { echo "<br><font color=\"#FFFFFF\">The item(s) is not in the cart. Add 1 to the cart</font><br>"; $cart($_GET['new']) => 1; } print_r($_SESSION['cart']); echo "<br>"; //echo "<font color=\"#FFFFFF\" \>the contents of the cart is now " . . "<br>"; //echo "<font color=\"#FFFFFF\">$cart[$new]</font><br>"; $total_price = calculate_price($cart); $items = calculate_items($cart); } if($save) { foreach ($cart as $itemSku => $qty) { if($$itemSku=="0") unset($cart[$itemSku]); else $cart[$itemSku] = $$itemSku; } $total_price = calculate_price($cart); $items = calculate_items($cart); } if($cart&&array_count_values($cart)) display_cart($cart); else echo "<p>There are no items in your cart"; //echo "<hr>"; $target = "index.php"; // if we have just added an item to the cart, continue shopping in that category if($new) { $details = get_item_details($new); if($details["catId"])
      $target = "products.php?catId=".$details["catId"]; } display_button("NULL", $target, "continue-shopping", "Continue Shopping");
      $path = $_SERVER['PHP_SELF']; //echo "$path"; $path = str_replace("cart.php", "", $path); display_button("NULL", "https://".$_SERVER['SERVER_NAME'].$path."checkout.php", "go-to-checkout", "Go To Checkout");
      //do_html_footer(); ?>

      I am sure it will most likely be something stupid but I just can't see it.

      Thanks

      Dave

        Write a Reply...