hi I'm hoping that someone here can help me I'm totally new to object oriented 
stuff and might be trying to do this totally ass backwards.
Having said that this also involves multidimensional arrays and I'm not to bright with 
these either . :)

I have looked on the web and FAQ's and manuals for help  but havenot found anything 
that quite 
covers this in this ..so I hope It's not so obvious that noone else has ever asked 
this.
Thanks in advance.

What I am trying to make is just a small shopping type application. My idea is 
for an Item code , an option for the item and the number wanted to be sent to a Cart 
Class
and stored as an multi dimensional array that has structure like this :

($productid, ($option => $qty))  
so that $option is a string index of the "second" array

or visually like this I think :
|          Code1        ||                Code 2               ||
| Option1 | Option2 || Option1 | Option2 | Option3 ||
|    QTY   |    QTY   ||    QTY   |    QTY  |   QTY    ||

What I am having problems with is setting the qty value correctly and updating it
Supposedly if you just keep hitting the update button then the qty should keep 
incrementing
but on the first run (for each code entered) it is zeroed ! and not 1 as is on the 
form .. after the first time all qty 
for options are set correctly, but I cannot figure out how to 'reach' into the array 
and update 
or even overwrite the values I am setting (overwriting is what I was expecting for the 
code below)


Thanks again 
Jamie Smith


CODE follows 
<?//This is self contained code just to show what I'm trying.

/* Start up the sessions,useing one array called MYSESSION to store all persistent 
variables.*/
/* In this example just my Cart info is stored but other customer preferences can be 
carried here later   */
session_start();
session_register("MYSESSION");

if (!isset($MYSESSION)) { /* initialize the MYSESSION variable */
 $MYSESSION = array();
}

/* initialize the CART objct if necessary */
if (! isset($MYSESSION["cart"])) {
 $MYSESSION["cart"] = new Cart;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
switch ($action) {
  case "clearall" :/*resets the array to null*/
   $MYSESSION["cart"]->init();
   header("Location: $HTTP_REFERER");
  break;
  case "add" :
 $MYSESSION["cart"]->add($item_code, $form_qty, $option);
  default:
?>
<html>
<body bgcolor="#FFFFFF">
<table align = "Center" width="300"><tr><td>
<form name="form" method="POST" >
  CODE : <input type="text" name="item_code" value="ABC"><br>
  Option:
    <input type="radio" name="option" value="A2" CHECKED>A2
    <input type="radio" name="option" value="A3"> A3
    <input type="radio" name="option" value="A4"> A4<br>
  Qty to add : <input type="text" name="form_qty" value="1">
  <input type="hidden" name="action" value="add">
    <input type="submit" name="Submit" value="Update">
</form>
<p><p><p>
<?
echo"Cart Contents :<br>\n<div align=\"right\"><a 
href=\"$PHP_SELF?action=clearall\">Clear All</a></div>";
$itemarray = $MYSESSION["cart"]->items;
while (list ($code) = each ($itemarray)){
 echo "<hr>\nCode : $code<br>\n<blockquote>\n";
 while(list($option,$qty) = each ($itemarray["$code"])){
  echo "Option = $option | QTY = $qty<br>\n";
 }
 echo"</blockquote>";
}
?></td></tr></table>
</body>
</html>
<?
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function setdefault(&$var, $default="")
{
/* if $var is undefined, set it to $default.  otherwise leave it alone */
 if (! isset($var)) {
  $var = $default;
 }
}

class Cart {
 var $items;  /* array of items ($productid, ($option => $qty))*/

 function Cart() {
 /* object constructor */
  $this->init();
 }

 function init() {
 /* this function is called to initialize (and reset) the cart */
  $this->items = array();
 }

 function add(&$productid, $qty, $option) {
 /* add an item to the shopping cart and update the total price */
  if (isset($productid)) {
   if(!($option))$option=0;/*not all products will have multiple options and may be 
passed as $productid and $qty*/
   $productinfoarray = array ($option => 0);
   setdefault($this->items[$productid],$productinfoarray);
   /* echo "Submitted : ".$productid." ".$option." ".$qty; */
   $this->items[$productid] += array($option => $qty);
  }
 }
}
?>

Reply via email to