"Rob Meade" <ku***************@edaem.bor> wrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
"Anthony Jones" wrote ...
Have you considered not using arrays but using XML instead. You can
safely
store a FreeThreadedDOMDocument in the session object.
Hi Anthony,
Thank you for your reply - I have to confess that I've not considered
using XML - at the time the array seemed the obvious way to go.
What would be the advantage of using XML? In addition -is this actually
an XML "file" or a string of XML elements? I only ask as potentially there
could be lots of carts being used at once and if files are involved I'm
not entirely sure how that would work.
Any further info is appreciated.
Regards
Rob
Sorry Rob I went offline for a few days. I think Mr Barrows has pretty much
covered it though.
Here's a really noddy example of an order entry page. (Note I'm just
demonstrating FreeThreadedDOM, avoiding other techniques and other good
practices a real world solution would use.)
Create an empty default.asp file in a folder somewhere and paste in the
below:-
<%
Option Explicit
Dim goOrderDOM
Dim goProductDOM
Dim goLine
Dim i
If IsEmpty(Session("OrderDOM")) Then
Set goOrderDOM = Server.CreateObject("MSXML2.FreeThreadedDOMDocumen t.3.0")
goOrderDOM.async = False
goOrderDOM.loadXML "<order />"
Set Session("OrderDOM") = goOrderDOM
Else
Set goOrderDOM = Session("OrderDOM")
End If
If IsEmpty(Application("ProductDOM")) Then
Set goProductDOM =
Server.CreateObject("MSXML2.FreeThreadedDOMDocumen t.3.0")
goProductDOM.async = False
goProductDOM.load Server.MapPath("Products.xml")
Set Application("ProductDOM") = goProductDOM
Else
Set goProductDOM = Application("ProductDOM")
End If
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then ProcessPost
%>
<html>
<head>
<title>Simple Order Entry</title>
</head>
<body>
<form method="POST" action="default.asp">
<table rules="all">
<thead>
<tr>
<th>Product</th><th>Quantity</th>
</tr>
</thead>
<tbody>
<%
i = 0
For Each goLine In goOrderDOM.documentElement.childNodes
%>
<tr>
<td><%=GetProductName(goLine)%></td>
<td class="quantity"><%=goLine.getAttribute("quantity" )%></td>
</tr>
<%
i = i + 1
Next
%>
<tr>
<td>
<select name="cboProduct">
<option>- Select Product -</option>
<%DrawProductOptions()%>
</select>
</td>
<td><input type="text" value="0" name="txtQty" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="Post" />
</form>
</body>
</html>
<%
Function GetProductName(roLine)
Dim sProdID
Dim oProduct
sProdID = roLine.getAttribute("productID")
Set oProduct = goProductDOM.selectSingleNode("//product[@ID=""" & sProdID &
"""]")
GetProductName = Server.HTMLEncode(oProduct.Text)
End Function
Sub DrawProductOptions
Dim oProd
For Each oProd in goProductDOM.documentElement.childNodes
Response.Write "<option value=""" & oProd.getAttribute("ID") & """>" & _
Server.HTMLEncode(oProd.Text) & "</option>"
Next
End Sub
Sub ProcessPost()
Dim sProdID
Dim lQty
Dim oLine
sProdID = Request.Form("cboProduct")
lQty = CLng(Request.Form("txtQty"))
If sProdID <> Empty Then
Set oLine = goOrderDOM.selectSingleNode("/order/line[@productID=""" &
sProdID & """]")
If lQty > 0 Then
If oLine Is Nothing Then
Set oLine =
goOrderDOM.documentElement.appendChild(goOrderDOM. createElement("line"))
oLine.setAttribute "productID", sProdID
End If
oLine.setAttribute "quantity", lQty
Else
If Not oLine Is Nothing Then
goOrderDOM.documentElement.removeChild(oLine)
End If
End If
End If
End Sub
%>
In the sample folder paste the following into a Products.xml file:-
<products>
<product ID="1">Widgets</product>
<product ID="2">Gizzmos</product>
<product ID="3">Thingamabobs</product>
</products>
That's it. Note that once a object holding the Order lines is added to the
session object it's never done again in that session. There's no need to
retrieve the object then put it back when finished (like you do with an
array). Most of the time a simple object pointer is retrieved from the
session object.
Note also the list of products is held in the application object. Hence
this in only done once for the whole application and all sessions can share
the same instance of the product list.
FWIW, I don't use standard HTML forms at all. If I were to develop an
application like this in the real world the order XML would be built client
side and only posted to the server on completion (XMLHTTPRequest). The
receiving ASP page might to do some pre-processing/validation of the XML
then passed to SQL Server batch or SP as a NTEXT parameter which is parsed
up with OPENXML.
Anthony.