Version: 1.1
Type: Class
Category: Shopping Carts
License: GNU General Public License
Description: A shopping cart which work with xml instead databases…
<? // XML Shopping cart // written by Riccardo Pasquini [email protected] // 29/11/2000 www.doublet.it/letsmeet define("__XCART__",1); if(!defined("__PIPE_PRICE__")) { include("PipePrice.iphp"); //u can find this class at www.phpbuilder.com in the shared cod library } class xProduct { //private var $m_nIndex; var $pID; // product unique id: could be the primary key of a table in a database.... var $m_nQuantity; // var $m_valCosto; // cost x unit //pubblico function xProduct($id,$quantita=-1,$costo_unitario=0) { $this->pID=$id; $this->m_nIndex=-1; $this->m_nQuantity=$quantita; $this->m_valCosto=$costo_unitario; } function xview() { $xstream="<prodotto>n"; $xstream.="<indice>".$this->m_nIndex."</indice>n"; $xstream.="<pid>".$this->pID."</pid>n"; $xstream.="<quantita>".$this->m_nQuantity."</quantita>n"; $xstream.="<uCostLit>".number_format($this->m_valCosto,0,",",".")."</uCostLit>n"; $xstream.="<uCostEu>".number_format($this->m_valCosto/1936.27,2,",",".")."</uCostEu>n"; $xstream.="</prodotto>n"; return $xstream; } } class xShoppingCart { //properties var $m_aProducts; //list of inserted products var $m_nCount; //number of inserted products var $m_valTotale; //totale del carrello //constructors function xShoppingCart() { $this->m_aProducts=array(); $this->m_nCount=0; } function xview($style=-1,$dtd=-1) //this function generate an xml page as a returned string the first parameter is a XSL file the second is a DTD { $pipe=new PipePrice(); $op=new Operation(); //null operation $pipe->addHeadOperation($op); //* $xstream="<?xml version="1.0"?>n"; if($style!=-1) $xstream.="<?xml:stylesheet href="$style" type="text/xsl"?>n"; if($dtd!=-1) $xstream.="<!DOCTYPE carrello SYSTEM "$dtd">n"; //*/ $xstream.="<carrello>n"; $totale=0; for($i=1;$i<=$this->m_nCount;$i++) //per ogni prodotto { $xstream.=$this->m_aProducts[$i-1]->xview(); $totale+=($this->m_aProducts[$i-1]->m_valCosto*$this->m_aProducts[$i-1]->m_nQuantity); } $pipe->setStartValue($totale); $totale=$pipe->getEndValue(); $xstream.="<totale>n"; $xstream.="<lire>".number_format($totale,0,",",".")."</lire>n"; $xstream.="<euro>".number_format($totale/1936.27,2,",",".")."</euro>n"; $xstream.="</totale>n"; $xstream.="</carrello>n"; return $xstream; } function addProduct(&$pr) //insert a new product { if($this->m_nCount==0) $this->m_aProducts=array(); $index=$this->chkCart($pr->pID); if($index!=-1) // gi nel carrello { $this->m_aProducts[$index]->m_nQuantity+=$pr->m_nQuantity; } else //non in carrello { $index=array_push($this->m_aProducts,$pr)-1; $this->m_aProducts[$this->m_nCount]->m_nIndex=$index; //save the product position in the cart into the m_nIndex product property $this->m_nCount++; } return $index; } function removeProduct($index) //the correctness of the index will be tested outside by the method { $pr=&$this->m_aProducts[$index]; if($this->m_nCount>1) { if($index==$this->m_nCount-1) //cancella l'ultimo { array_pop($this->m_aProducts); } else { $this->m_aProducts[$index]=&array_pop($this->m_aProducts); $this->m_aProducts[$index]->m_nIndex=$index; } } else array_pop($this->m_aProducts); $this->m_nCount--; return $pr; } function emptyCart() { for($i=0;$i<$this->m_nCount;$i++) $this->removeProduct(0); $this->m_nCount=0; } function changeQuantity($index,$nQuantity) //the correctness of the index will be tested outside by the method { $this->m_aProducts[$index]->m_nQuantity=$nQuantity; return $this->m_aProducts[$index]; } //private methods function chkCart($id) { for($i=0;$i<$this->m_nCount;$i++) if($this->m_aProducts[$i]->pID==$id) return $i; return -1; } }; ?>