title: Coldfusion Arrays and Session shopping cart
body: <p>I'm a noob with Coldfusion and I've been trying to figure out this simple issue for weeks now.  I have a shopping cart i've created using coldfusion and dreamweaver. I'm trying to figure out what I've got wrong in my Application.CFC that's not deleting the session on browser close or reload.. Any guidence would be greatly appreciated. TIA  </p>
  
  <p><strong>APPLICATION.CFC Code:</strong> <strong>Edited 4/19/2016 @ 10:55pm</strong></p>
  
  <pre><code>&lt;cfcomponent&gt; 
  &lt;cfset this.name = "cart"&gt; 
  &lt;cfset application.datasource.name = "mmd24_shoppingcart"&gt;
  &lt;cfset application.directory.root.path = "productlist.cfm" &gt; 
  &lt;cfset This.Sessionmanagement=True&gt;
  &lt;cfset This.Sessiontimeout="#createtimespan(0,5,0,0)#"&gt;
  &lt;cfset This.applicationtimeout="#createtimespan(5,0,0,0)#"&gt;
  
  &lt;cfif isdefined("cookie.CFID") and isdefined("cookie.CFTOKEN")&gt;
  &lt;cfset tempCFID = cookie.CFID &gt;
  &lt;cfset tempCFTOKEN = cookie.cftoken &gt;
  &lt;cfcookie name="CFID"  value="#tempCFID#" &gt;
  &lt;cfcookie name="CFTOKEN"  value="#tempCFTOKEN#" &gt;
  &lt;/cfif&gt; 
  &lt;/cfcomponent&gt;
  </code></pre>
  
  <p><strong>shoppingcart.cfm</strong> <strong>EDITED 4/19/2016 @ 10:55pm CST</strong></p>
  
  <pre><code>&lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
  
  &lt;html&gt;
  &lt;html lang="en"&gt;
  &lt;meta name="viewport" content="width=device-width. initial-scale=1 
  charset=utf-8&gt;
  
  &lt;cfparam name = "url.productid" default = ""&gt;
  &lt;cfparam name = "url.qty" default = ""&gt;
  
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap
  /3.3.6/css/bootstrap.min.css"&gt;
  &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap
  /3.3.6/css/bootstrap-theme.min.css"&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3
  /jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6
  /js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;style&gt; 
  p.solid{border-style: solid;}
  text.solid{border-style: solid;}
  text.inset {border-style: inset;}
  text.double {border-style: double;}
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
   &lt;br /&gt;
   &lt;center&gt;&lt;h1&gt;&lt;p&gt; ShoppingCart&lt;/p&gt;&lt;/h1&gt;&lt;/center&gt;
  &lt;cfquery name = "getlist" datasource="mmd24_shoppingcart"&gt;
                SELECT * 
                FROM Products
                Where Products.productID = &lt;cfqueryparam  
                cfsqltype="cf_sql_integer" value="#url.productid#"&gt;
  &lt;/cfquery&gt;
  
  &lt;cflock scope="session" timeout="10"&gt;   
  
          &lt;cfset addNew = true&gt;
  
          &lt;cfif not isDefined("session.cart")&gt;
  
          &lt;cfset session.cart = arrayNew(1)&gt;
  
          &lt;cfelse&gt;
  
          &lt;cfloop index="i" from="1" to="#arrayLen(session.cart)#"&gt;
  
              &lt;cfif URL.productid is session.cart[i].productId&gt;
  
              &lt;cfset session.cart[i].productQty = 
               session.cart[i].productQty + 1&gt;
  
           &lt;cfset addNew = false&gt;
                         &lt;/cfif&gt;
                     &lt;/cfloop&gt;
  
                  &lt;/cfif&gt;
  
                  &lt;cfif addNew is true&gt;
  
                     &lt;cfset newItem=StructNew()&gt;
  
                     &lt;cfset newItem.productId = URL.productid&gt;
  
                     &lt;cfset newItem.productName = getlist.ProductName&gt;
  
                     &lt;cfset newItem.productPrice = getlist.ProductPrice&gt;
  
                     &lt;cfset newItem.productDescription = 
                      getlist.ProductDescription&gt;
  
                     &lt;cfset newItem.productQty = URL.qty&gt;
  
                     &lt;cfset newItem.productPhoto = getlist.ProductPhoto&gt;
  
                     &lt;cfset ArrayAppend(session.cart, newItem)&gt;
  
                  &lt;/cfif&gt;        
  
      &lt;cfset TotalOrderAmount = 0&gt;
      &lt;cfset TotalItemAmount = 0&gt;
      &lt;cfset TotalTax = 0&gt;
      &lt;cfset counterhi = 0&gt;
  &lt;/cflock&gt;
  
  &lt;cfoutput query ="getlist"&gt;
  
  
  &lt;cflock scope="session" type="readonly" timeout="10"&gt;
  
      &lt;cfloop index="i" from="1" to="#arrayLen(session.cart)#"&gt;
  
  &lt;table class = "table table-bordered"&gt;
  &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;Product Photo&lt;/th&gt;
          &lt;th&gt;Product Name&lt;/th&gt;
          &lt;th&gt;Product Description&lt;/th&gt;
          &lt;th&gt;Quantity Ordered&lt;/th&gt;
          &lt;th&gt;Product Price&lt;/th&gt;
        &lt;/th&gt;
  &lt;/thead&gt;
  
  &lt;tr&gt;
      &lt;td&gt;&lt;img src="#session.cart[i].ProductPhoto#"&gt;&lt;/td&gt;
      &lt;td&gt;#session.cart[i].ProductName#&lt;/td&gt;
      &lt;td&gt;#session.cart[i].ProductDescription#&lt;/td&gt;
      &lt;td&gt;#session.cart[i].ProductQty#&lt;/td&gt;
      &lt;td&gt;#DollarFormat(session.cart[i].ProductPrice)#&lt;/td&gt;
  
      &lt;cfset Itemtotal = #getlist.productprice# * 
       #session.cart[i].ProductQty#&gt;
      &lt;cfset OrderTotal = #Itemtotal#&gt;
      &lt;cfset Tax = #OrderTotal# * "0.07"&gt;
      &lt;cfset TotalOrderAmount = #OrderTotal# + #Tax# + #TotalOrderAmount#&gt;
      &lt;cfset TotalItemAmount = #Itemtotal# + #TotalItemAmount#&gt;
      &lt;cfset TotalTax = #Tax# + #TotalTax#&gt;
      &lt;tr&gt;
      &lt;td&gt; &lt;b&gt;&lt;text class=double&gt;Item Total:&lt;/text&gt;&lt;/b&gt;&lt;text 
      class=double&gt;#DollarFormat(Itemtotal)# &lt;/text&gt;&lt;br /&gt;
      &lt;b&gt;&lt;text class=double&gt;Sales Tax:&lt;/text&gt;&lt;/b&gt;&lt;text 
      class=double&gt;#DollarFormat(Tax)# &lt;/text&gt;&lt;br /&gt;
      &lt;/td&gt;
      &lt;/table&gt;
  
  &lt;/cfloop&gt;
  &lt;/cflock&gt;
  &lt;/cfoutput&gt; 
  
          &lt;cfoutput query ="getlist"&gt;
      &lt;br /&gt; &lt;br /&gt; &lt;br /&gt;
      &lt;b&gt;&lt;text class=double&gt;Total Item Amount:&lt;/text&gt;&lt;/b&gt;&lt;text 
      class=double&gt; #DollarFormat(TotalItemAmount)# &lt;/text&gt;&lt;br/&gt;
      &lt;b&gt;&lt;text class=double&gt;Total Tax Amount:&lt;/text&gt;&lt;/b&gt;&lt;text 
      class=double&gt; #DollarFormat(TotalTax)# &lt;/text&gt;&lt;br/&gt;
      &lt;b&gt;&lt;text class=double&gt;Total Order Amount:&lt;/text&gt;&lt;/b&gt;&lt;text 
      class=double&gt; #DollarFormat(TotalOrderAmount)# &lt;/text&gt;&lt;br/&gt;
      &lt;br /&gt;&lt;br /&gt;
      &lt;form action="customerform.cfm" method="post"&gt;
      &lt;a input type="submit" name="submit" href="customerform.cfm" 
      class="btn btn-primary" role="button"&gt;Proceed to Checkout&lt;/a&gt;
      &lt;br&gt;
      &lt;/br&gt;
      &lt;a input type="submit" name="submit" href="productlist.cfm" 
      class="btn btn-primary" role="button"&gt;Continue Shopping&lt;/a&gt;
      &lt;br /&gt;
  &lt;/cfoutput&gt; 
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>