Coldfusion阵列和会话购物车

时间:2016-04-16 21:01:59

标签: arrays coldfusion dreamweaver

我是Coldfusion的小伙伴,我一直试图找出这个简单的问题好几周了。我有一个使用coldfusion和dreamweaver创建的购物车。我正在试图弄清楚我的Application.CFC中出现了什么问题,而不是在浏览器关闭或重新加载时删除会话。任何指导都将非常感谢。 TIA

APPLICATION.CFC代码: 2016年4月19日编辑@ 10:55

<cfcomponent> 
<cfset this.name = "cart"> 
<cfset application.datasource.name = "mmd24_shoppingcart">
<cfset application.directory.root.path = "productlist.cfm" > 
<cfset This.Sessionmanagement=True>
<cfset This.Sessiontimeout="#createtimespan(0,5,0,0)#">
<cfset This.applicationtimeout="#createtimespan(5,0,0,0)#">

<cfif isdefined("cookie.CFID") and isdefined("cookie.CFTOKEN")>
<cfset tempCFID = cookie.CFID >
<cfset tempCFTOKEN = cookie.cftoken >
<cfcookie name="CFID"  value="#tempCFID#" >
<cfcookie name="CFTOKEN"  value="#tempCFTOKEN#" >
</cfif> 
</cfcomponent>

shoppingcart.cfm EDITED 4/19/2016 @ 10:55 pm CST

<link href="css/bootstrap.min.css" rel="stylesheet">

<html>
<html lang="en">
<meta name="viewport" content="width=device-width. initial-scale=1 
charset=utf-8>

<cfparam name = "url.productid" default = "">
<cfparam name = "url.qty" default = "">

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap
/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap
/3.3.6/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3
/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6
/js/bootstrap.min.js"></script>
<style> 
p.solid{border-style: solid;}
text.solid{border-style: solid;}
text.inset {border-style: inset;}
text.double {border-style: double;}
</style>
</head>

<body>

 <br />
 <center><h1><p> ShoppingCart</p></h1></center>
<cfquery name = "getlist" datasource="mmd24_shoppingcart">
              SELECT * 
              FROM Products
              Where Products.productID = <cfqueryparam  
              cfsqltype="cf_sql_integer" value="#url.productid#">
</cfquery>

<cflock scope="session" timeout="10">   

        <cfset addNew = true>

        <cfif not isDefined("session.cart")>

        <cfset session.cart = arrayNew(1)>

        <cfelse>

        <cfloop index="i" from="1" to="#arrayLen(session.cart)#">

            <cfif URL.productid is session.cart[i].productId>

            <cfset session.cart[i].productQty = 
             session.cart[i].productQty + 1>

         <cfset addNew = false>
                       </cfif>
                   </cfloop>

                </cfif>

                <cfif addNew is true>

                   <cfset newItem=StructNew()>

                   <cfset newItem.productId = URL.productid>

                   <cfset newItem.productName = getlist.ProductName>

                   <cfset newItem.productPrice = getlist.ProductPrice>

                   <cfset newItem.productDescription = 
                    getlist.ProductDescription>

                   <cfset newItem.productQty = URL.qty>

                   <cfset newItem.productPhoto = getlist.ProductPhoto>

                   <cfset ArrayAppend(session.cart, newItem)>

                </cfif>        

    <cfset TotalOrderAmount = 0>
    <cfset TotalItemAmount = 0>
    <cfset TotalTax = 0>
    <cfset counterhi = 0>
</cflock>

<cfoutput query ="getlist">


<cflock scope="session" type="readonly" timeout="10">

    <cfloop index="i" from="1" to="#arrayLen(session.cart)#">

<table class = "table table-bordered">
<thead>
    <tr>
        <th>Product Photo</th>
        <th>Product Name</th>
        <th>Product Description</th>
        <th>Quantity Ordered</th>
        <th>Product Price</th>
      </th>
</thead>

<tr>
    <td><img src="#session.cart[i].ProductPhoto#"></td>
    <td>#session.cart[i].ProductName#</td>
    <td>#session.cart[i].ProductDescription#</td>
    <td>#session.cart[i].ProductQty#</td>
    <td>#DollarFormat(session.cart[i].ProductPrice)#</td>

    <cfset Itemtotal = #getlist.productprice# * 
     #session.cart[i].ProductQty#>
    <cfset OrderTotal = #Itemtotal#>
    <cfset Tax = #OrderTotal# * "0.07">
    <cfset TotalOrderAmount = #OrderTotal# + #Tax# + #TotalOrderAmount#>
    <cfset TotalItemAmount = #Itemtotal# + #TotalItemAmount#>
    <cfset TotalTax = #Tax# + #TotalTax#>
    <tr>
    <td> <b><text class=double>Item Total:</text></b><text 
    class=double>#DollarFormat(Itemtotal)# </text><br />
    <b><text class=double>Sales Tax:</text></b><text 
    class=double>#DollarFormat(Tax)# </text><br />
    </td>
    </table>

</cfloop>
</cflock>
</cfoutput> 

        <cfoutput query ="getlist">
    <br /> <br /> <br />
    <b><text class=double>Total Item Amount:</text></b><text 
    class=double> #DollarFormat(TotalItemAmount)# </text><br/>
    <b><text class=double>Total Tax Amount:</text></b><text 
    class=double> #DollarFormat(TotalTax)# </text><br/>
    <b><text class=double>Total Order Amount:</text></b><text 
    class=double> #DollarFormat(TotalOrderAmount)# </text><br/>
    <br /><br />
    <form action="customerform.cfm" method="post">
    <a input type="submit" name="submit" href="customerform.cfm" 
    class="btn btn-primary" role="button">Proceed to Checkout</a>
    <br>
    </br>
    <a input type="submit" name="submit" href="productlist.cfm" 
    class="btn btn-primary" role="button">Continue Shopping</a>
    <br />
</cfoutput> 

</body>
</html>

2 个答案:

答案 0 :(得分:4)

FWIW,您永远不应该提到价格和其他信息的用户输入(URLFORM范围)。您应该获取产品的ID并查找数据库中的信息,以便将其存储在session中。最好的情况是,您要从用户输入中提取产品ID和数量,确保为每个输入设置了正确的cfparam

答案 1 :(得分:1)

具体到错误,可能来自这一行:

<cfset sItem.price = url.ProductPrice>

由于您没有使用cfparam为url.ProductPrice设置默认值,如果每次都没有将ProductPrice传递给via URL,那么它将无法找到它。也许您需要为每个可能的url var使用cfparam。