[ASP/vbScript] Shopping Cart - removing items by quantity - theory..

Discussion in 'ASP General' started by Rob Meade, Jun 13, 2006.

  1. Rob Meade

    Rob Meade Guest

    Hi all,

    Ok - so I've got the array thing going on, and the session thing going on,
    and up until now its all been ok.

    I've got my view basket page which is displaying 3 rows (as an example) - I
    have an input box on each row enabling the user to update the quantity.

    If the enter a zero I need to remove the item from the array....

    Without getting too much into code - I was hoping for an "in words" reply as
    to how to get around my problem...

    I have 2 loops - one outer, one inner, which are iterating through 2 arrays,
    the outer array is that of the "quantities" as posted by the form, the inner
    array is that of the items in the basket as populated from the session.

    Now - I've tried implementing my code that removes items from the array, but
    I've discovered a problem....because I need to remove the item I end up
    using a temporary array to store the items that are NOT being removed, then
    I resize the original array and pop 'em back in. Because the size of the
    original array has now changed, if there are more than one item in the
    basket which are being removed (by entering a zero for quantity) when it
    gets to the next zero to deal with, the index has changed on the array -
    thus buggering my loops a tad...

    Any thoughts/theories on the approach to this - again, ideally in words
    rather than code - its late and my eyes hurt :eek:(

    Thanks in advance for any help.

    Regards

    Rob
     
    Rob Meade, Jun 13, 2006
    #1
    1. Advertising

  2. Rob Meade

    Rob Meade Guest

    oooh...dont you just hate it when you have a good idea you think might be
    the solution just AFTER posting to the group (or am I the only one that does
    this - still nice to talk to myself!)..

    Ok - how's this sound...

    I iterate through the outer array (items in the basket), and the inner array
    (quantities) - I update the quantities accordingly...

    Then, I iterate through the basket items array again, this time, taking
    products from it where the quantity is greater than zero and placing them
    into a new array.

    At the end of it I should have an array with just the basket items with a
    quantity > 0 ...

    Sound ok?

    Regards

    Rob
     
    Rob Meade, Jun 13, 2006
    #2
    1. Advertising

  3. "Rob Meade" <> wrote in message
    news:MzGjg.85522$...
    > Hi all,
    >
    > Ok - so I've got the array thing going on, and the session thing going on,
    > and up until now its all been ok.
    >
    > I've got my view basket page which is displaying 3 rows (as an example) -

    I
    > have an input box on each row enabling the user to update the quantity.
    >
    > If the enter a zero I need to remove the item from the array....
    >
    > Without getting too much into code - I was hoping for an "in words" reply

    as
    > to how to get around my problem...
    >
    > I have 2 loops - one outer, one inner, which are iterating through 2

    arrays,
    > the outer array is that of the "quantities" as posted by the form, the

    inner
    > array is that of the items in the basket as populated from the session.
    >
    > Now - I've tried implementing my code that removes items from the array,

    but
    > I've discovered a problem....because I need to remove the item I end up
    > using a temporary array to store the items that are NOT being removed,

    then
    > I resize the original array and pop 'em back in. Because the size of the
    > original array has now changed, if there are more than one item in the
    > basket which are being removed (by entering a zero for quantity) when it
    > gets to the next zero to deal with, the index has changed on the array -
    > thus buggering my loops a tad...
    >
    > Any thoughts/theories on the approach to this - again, ideally in words
    > rather than code - its late and my eyes hurt :eek:(
    >
    > Thanks in advance for any help.
    >
    > Regards
    >
    > Rob
    >


    Have you considered not using arrays but using XML instead. You can safely
    store a FreeThreadedDOMDocument in the session object.
     
    Anthony Jones, Jun 14, 2006
    #3
  4. Rob Meade

    Rob Meade Guest

    "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
     
    Rob Meade, Jun 14, 2006
    #4
  5. Rob Meade

    Bob Lehmann Guest

    >> What would be the advantage of using XML?
    XML has a higher "cool factor".

    The original intent behind XML was that it is simply text, used to
    communicate between disparate systems, especially via HTTP.

    Now-a-days, even if your application only communicates with itself, if your
    app doesn't use XML, you run the risk of being found guilty of non-buzzword
    compliance by the PHB or someone in Marketing & Sales.

    Bob Lehmann

    "Rob Meade" <> wrote in message
    news:e$...
    > "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
    >
    >
     
    Bob Lehmann, Jun 15, 2006
    #5
  6. Rob Meade

    Rob Meade Guest

    "Bob Lehmann" wrote...

    >>> What would be the advantage of using XML?

    > XML has a higher "cool factor".


    ROTFL!

    Nice reply :eek:D

    I was expecting a reply more a long the lines of "arrays are more costly on
    the servers resources" or something like that - hehe.. :eek:)

    Rob
     
    Rob Meade, Jun 16, 2006
    #6
  7. Rob Meade

    Bob Lehmann Guest

    > I was expecting a reply more a long the lines of "arrays are more costly

    :>)

    Invariably, the usual flow with XML is ...

    Load the XML document
    Parse and loop through the tags and their children
    Place values in an array
    Loop through the array
    Process the data

    Cool!

    I'm not saying that XML doesn't have its place, but it *is* highly overused
    / abused.

    Bob Lehmann

    "Rob Meade" <> wrote in message
    news:Jmmkg.86783$...
    > "Bob Lehmann" wrote...
    >
    > >>> What would be the advantage of using XML?

    > > XML has a higher "cool factor".

    >
    > ROTFL!
    >
    > Nice reply :eek:D
    >
    > I was expecting a reply more a long the lines of "arrays are more costly

    on
    > the servers resources" or something like that - hehe.. :eek:)
    >
    > Rob
    >
    >
     
    Bob Lehmann, Jun 16, 2006
    #7
  8. Rob Meade wrote:
    > "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?


    1. Using the selectSingleNode and selectNodes methods combined with XPATH
    queries, xml domdocuments () aka "xml documents" for shortare easily
    searchable.
    2. Nodes can easily be removed from and inserted into the body of the xml
    document.

    If you don't need either of these functions (as well as the other
    functionality provided by the MS XML Parser), then use arrays. However, in
    your case, xml documents seem to be what you need.

    > In addition -is this
    > actually an XML "file" or a string of XML elements?


    Neither.
    No files need to be involved, although they can be.
    An xml document is an object provided by the MS XML Parser library. It has
    properties, collections and methods which can be used to work with the xml
    contained in the document object. Its free-threaded version can be safely
    stored in session or application. It has a Save method which can be used to
    save its xml to a file, or to stream it to Response or an ADO Stream
    (allowing it to be serialized into a recordset object). ADO recordsets also
    have a Save method which allow their contents, metadata and data, to be
    saved to a file as xml, or streamed into an xml document. I have an example
    of this here:
    http://www.davidpenton.com/testsite/tips/xml.recordset.server2client.asp

    Here is another example of using xml documents:
    http://www.davidpenton.com/testsite/tips/xml.data.islands.asp


    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Jun 16, 2006
    #8
  9. Bob Lehmann wrote:
    >> I was expecting a reply more a long the lines of "arrays are more
    >> costly

    >
    >>> )

    >
    > Invariably,


    Invariable? :)

    > the usual flow with XML is ...
    >
    > Load the XML document
    > Parse and loop through the tags and their children
    > Place values in an array


    Huh??
    Why would anyone ever do that?

    I think you're exaggerating here to be funny.
    I have made extensive use of xml documents and not once have I ever moved
    data from a document into an array.
    Neither have I ever seen any examples of anyone doing this.
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Jun 16, 2006
    #9
  10. "Rob Meade" <> wrote in message
    news:e$...
    > "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.FreeThreadedDOMDocument.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.FreeThreadedDOMDocument.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.
     
    Anthony Jones, Jun 18, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGhucml2ZXJzaWRl?=

    cookieless asp.net shopping cart code/how to

    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Aug 14, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=
    Aug 14, 2005
  2. alex sparsky
    Replies:
    3
    Views:
    480
    George
    Jan 5, 2006
  3. semut

    Shopping cart ASP.NET

    semut, May 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    459
    clintonG
    May 30, 2006
  4. JJ
    Replies:
    2
    Views:
    425
    Ken Cox [Microsoft MVP]
    Jul 31, 2006
  5. Andy B
    Replies:
    2
    Views:
    336
    Peter Bromberg [C# MVP]
    Nov 24, 2007
Loading...

Share This Page