topical media & game development

talk show tell print

basic-ajax-08-cartdisplay.php / php



  <?php
  session_register('Cart');
  header('Content-Type: text/xml');
  ?>
  <?php
          newitem = _POST["book"];
          action = _POST["action"];
          if (_SESSION["Cart"] != "")
          {
              MDA = _SESSION["Cart"];
              if (action == "Add")
              {
                  if (MDA[newitem] != "")
                  {  
                      value = MDA[newitem] + 1;
                      MDA[newitem] = value;
                      _SESSION["Cart"] = MDA; 
                      ECHO (toXml(MDA));    
                  }
                  else
                  {
                      MDA[newitem] = "";
                      _SESSION["Cart"] = MDA;
                      ECHO (toXml(MDA));
                  }
              }
              else
              {
                  value = MDA[newitem];
                  MDA= "";
                  _SESSION["Cart"] = "";   
                  ECHO (toXml(MDA));
              }
          }
          else
          {
              MDA[newitem] = "1";
              _SESSION["Cart"] = MDA;
              ECHO (toXml(MDA));
          }
                                                                          
      
     function toXml(MDA)
      {
          doc = new DomDocument('1.0');
          cart = doc->appendChild(doc->createElement('cart'));
          total = 0;
          
          foreach (MDA as Item => ItemName)
          {
              book = cart->appendChild(doc->createElement('book'));
      
              title = doc->appendChild(doc->createElement('Title'));
              title->appendChild(doc->createTextNode(Item));
              
              authors = book->appendChild(doc->createElement('Authors'));
              authors->appendChild(doc->createTextNode(_POST['authors']));
              
              isbn = book->appendChild(doc->createElement('ISBN'));
              isbn->appendChild(doc->createTextNode(_POST['ISBN']));
              
              price = str_replace("","",_POST['price']);
              priceNode = book->appendChild(doc->createElement('Price'));
              priceNode->appendChild(doc->createTextNode(price));
      
              quantity = book->appendChild(doc->createElement('Quantity'));
              quantity->appendChild(doc->createTextNode(ItemName));
              
              book->appendChild(title);
              book->appendChild(quantity);
              book->appendChild(authors);
              book->appendChild(isbn);
              book->appendChild(priceNode);
              
              total = price * ItemName;
        }
        
         totalNode =  cart->appendChild(doc->createElement('Total'));
         totalNode->appendChild(doc->createTextNode(total));
  
         strXml = doc->saveXML(); 
         return strXml;
      }
  
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.