Hello
I am building a website with an order list. This order list is similar to the basket of an ecommerce website it stores products to be used once the person has finished adding things. They then submit the order to us for processing.
I am using a session variable to capture and retain the information, but I am having sooooo much trouble with it i'm tempted to ditch the lot.
The code basically pulls the product information from the DB based on the ID stored in the URL. It then populates an array in a structure called Item. This array then retains all the information and can grow or shrink depending on the persons preference.
I opened the page to test it in firefox (and obviously the session/array wasn't defined) so I got an error. So I used Cfparam tags to define the values that it said were missing but I still get this error:
--------------------------
----------
----------
----------
----------
---|
Element ORDERLIST.ITEM is undefined in SESSION.
The error occurred in C:\Inetpub\wwwroot\
www.thechalkboardcompany.co.uk\htdocs\orderList.cfm: line 64
--------------------------
----------
----------
----------
----------
---|
All I want to do is be able to view the page without defining the Array because that should only be defined if someone adds something to it. Otherwise it should remain blank and display a message saying there's nothing here etc etc.
Ok enough rabbiting on here is the code:
--------------------------
----------
----------
----------
----------
---|
<cfif URL.mode eq "add" and URL.prodID is not "" or URL.mode eq "set" and URL.prodID is not "">
<cfquery name="getProducts" datasource="#dsl#">
SELECT * FROM products WHERE productID=#URL.prodID#
</cfquery>
</cfif>
<cfif URL.mode eq "add" and URL.prodID is not "">
<cfif not isDefined("session.orderLi
st")>
<cfset session.orderList = StructNew()>
<cfset session.orderList.Item = ArrayNew(1)>
</cfif>
<cfset listNumber = ArrayLen(session.orderList
.Item) +1>
<cfset session.orderList.Item[lis
tNumber] = StructNew()>
<cfset session.orderList.Item[lis
tNumber].p
roductName
= getProducts.product>
<cfset session.orderList.Item[lis
tNumber].p
roductID = getProducts.productID>
<cfset session.orderList.Item[lis
tNumber].q
ty = getProducts.minimumQty>
<cfset session.orderList.Item[lis
tNumber].c
ostPerItem
= getProducts.costPerItem>
<cfset session.orderList.Item[lis
tNumber].t
otal = session.orderList.Item[lis
tNumber].q
ty * session.orderList.Item[lis
tNumber].c
ostPerItem
>
<cflocation url="#thisPage#" addtoken="no">
<cfelseif mode eq "set" and URL.ID is not "">
<cfif FORM.qty ge getProducts.minimumQty>
<cfset session.orderList.Item[URL
.ID].qty = FORM.qty>
<cfset session.orderList.Item[URL
.ID].total
= session.orderList.Item[URL
.ID].qty * session.orderList.Item[URL
.ID].costP
erItem>
</cfif>
<cflocation url="#thisPage#" addtoken="no">
<cfelseif mode eq "del" and URL.prodID is not "">
<cfset arrayDeleteAt(session.orde
rList.Item
, URL.ID)>
<cflocation url="#thisPage#" addtoken="no">
<cfelseif mode eq "delAll">
<cfset clearStruct = structClear(session.orderL
ist)>
<cflocation url="#thisPage#" addtoken="no">
</cfif>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Chalkboard Order List</title>
<link rel="stylesheet" href="/m.css" type="text/css" media="all">
<script language="javascript1.1" type="text/javascript">
function decision(message, url){
if(confirm(message)) location.href = url;
}
</script>
</head>
<body>
<cfinclude template="/incHeader.cfm">
<div id="titleBar">
<h2>Your Order List</h2>
</div>
<cfinclude template="/incUtilityBar.c
fm">
<div id="container">
<cfinclude template="/incLeftNav.cfm"
>
<div id="content">
<div class="floatLeft">
<img src="img/content/headerRig
ht.jpg" width="11" height="30" alt="" border="0" class="floatRight">
<img src="img/content/headerLef
t.jpg" width="11" height="30" alt="" border="0" class="floatLeft">
<h2>Your Order List</h2>
<table cellpadding="2" cellspacing="0" border="0" width="100%">
<cfif isDefined("session.orderLi
st")>
<cfset orderTotal = 0>
<cfloop index="idx" from="1" to="#ArrayLen(session.orde
rList.Item
)#">
<cfif idx mod 2>
<cfset class="rowa">
<cfelse>
<cfset class="rowb">
</cfif>
<cfoutput>
<tr>
<td width="1"><a href="##" onClick="decision('Are you sure you want to delete this item?','<cfoutput>#thisPag
e#?mode=de
l&ID=#idx#
&prodID=#s
ession.ord
erList.Ite
m[idx].pro
ductID#</c
foutput>')
"><img src="/img/common/delete.gi
f" alt="Click here to delete the quote item" width="15" height="15" border="0"></a></td>
<td>#session.orderList.Ite
m[idx].pro
ductName#<
/td>
<td>#session.orderList.Ite
m[idx].pro
ductID#</t
d>
<td>#session.orderList.Ite
m[idx].qty
#</td>
<td>#session.orderList.Ite
m[idx].cos
tPerItem#<
/td>
<td>#session.orderList.Ite
m[idx].tot
al#</td>
<td><form action="#thisPage#?mode=se
t&ID=#idx#
&prodID=#s
ession.ord
erList.Ite
m[idx].pro
ductID#" method="post"><input type="text" name="qty" value="#session.orderList.
Item[idx].
qty#" size="5"><input type="submit" value="update"></form></td
>
</tr>
</cfoutput>
<cfset orderTotal = orderTotal + session.orderList.Item[idx
].total>
</cfloop>
<cfset session.orderList.orderTot
al = orderTotal>
<tr>
<td colspan="7" align="right">
<cfoutput>#orderTotal#</cf
output>
</td>
</tr>
<cfelse>
<tr>
<td align="center">There are no items in your order list.</td>
</tr>
</cfif>
</table>
<a href="<cfoutput>#thisPage#
?mode=delA
ll</cfoutp
ut>">Delet
e all</a>
</div>
</div>
<br clear="all">
<div id="categoryFooter"><img src="img/leftNav/footer.gi
f" width="185" height="11" alt="" border="0"></div>
<div id="contentFooter">
<img src="img/content/footerRig
ht.gif" width="11" height="11" alt="" border="0" class="floatRight">
<img src="img/content/footerLef
t.gif" width="11" height="11" alt="" border="0" class="floatLeft">
</div>
</div>
</body>
</html>
--------------------------
----------
----------
----------
----------
---|