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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Meade

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

    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....beca use 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 :o(

    Thanks in advance for any help.

    Regards

    Rob


  • Rob Meade

    #2
    Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

    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


    Comment

    • Anthony Jones

      #3
      Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..


      "Rob Meade" <ten.bewdoowsgn ikNO-SPAM@edaem.bbor > wrote in message
      news:MzGjg.8552 2$wl.43776@text .news.blueyonde r.co.uk...[color=blue]
      > 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) -[/color]
      I[color=blue]
      > 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[/color]
      as[color=blue]
      > to how to get around my problem...
      >
      > I have 2 loops - one outer, one inner, which are iterating through 2[/color]
      arrays,[color=blue]
      > the outer array is that of the "quantities " as posted by the form, the[/color]
      inner[color=blue]
      > 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,[/color]
      but[color=blue]
      > I've discovered a problem....beca use I need to remove the item I end up
      > using a temporary array to store the items that are NOT being removed,[/color]
      then[color=blue]
      > 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 :o(
      >
      > Thanks in advance for any help.
      >
      > Regards
      >
      > Rob
      >[/color]

      Have you considered not using arrays but using XML instead. You can safely
      store a FreeThreadedDOM Document in the session object.


      Comment

      • Rob Meade

        #4
        Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

        "Anthony Jones" wrote ...
        [color=blue]
        > Have you considered not using arrays but using XML instead. You can
        > safely
        > store a FreeThreadedDOM Document in the session object.[/color]

        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


        Comment

        • Bob Lehmann

          #5
          Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

          >> 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" <ku.shn.tsews.t hbu@edaem.bor> wrote in message
          news:e$Mdpt7jGH A.1324@TK2MSFTN GP04.phx.gbl...[color=blue]
          > "Anthony Jones" wrote ...
          >[color=green]
          > > Have you considered not using arrays but using XML instead. You can
          > > safely
          > > store a FreeThreadedDOM Document in the session object.[/color]
          >
          > Hi Anthony,
          >
          > Thank you for your reply - I have to confess that I've not considered[/color]
          using[color=blue]
          > 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[/color]
          an[color=blue]
          > 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[/color]
          not[color=blue]
          > entirely sure how that would work.
          >
          > Any further info is appreciated.
          >
          > Regards
          >
          > Rob
          >
          >[/color]


          Comment

          • Rob Meade

            #6
            Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

            "Bob Lehmann" wrote...
            [color=blue][color=green][color=darkred]
            >>> What would be the advantage of using XML?[/color][/color]
            > XML has a higher "cool factor".[/color]

            ROTFL!

            Nice reply :oD

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

            Rob


            Comment

            • Bob Lehmann

              #7
              Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

              > 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" <ten.bewdoowsgn ikNO-SPAM@edaem.bbor > wrote in message
              news:Jmmkg.8678 3$wl.59898@text .news.blueyonde r.co.uk...[color=blue]
              > "Bob Lehmann" wrote...
              >[color=green][color=darkred]
              > >>> What would be the advantage of using XML?[/color]
              > > XML has a higher "cool factor".[/color]
              >
              > ROTFL!
              >
              > Nice reply :oD
              >
              > I was expecting a reply more a long the lines of "arrays are more costly[/color]
              on[color=blue]
              > the servers resources" or something like that - hehe.. :o)
              >
              > Rob
              >
              >[/color]


              Comment

              • Bob Barrows [MVP]

                #8
                Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

                Rob Meade wrote:[color=blue]
                > "Anthony Jones" wrote ...
                >[color=green]
                >> Have you considered not using arrays but using XML instead. You can
                >> safely
                >> store a FreeThreadedDOM Document in the session object.[/color]
                >
                > 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?[/color]

                1. Using the selectSingleNod e 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.
                [color=blue]
                > In addition -is this
                > actually an XML "file" or a string of XML elements?[/color]

                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:


                Here is another example of using xml documents:



                --
                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"


                Comment

                • Bob Barrows [MVP]

                  #9
                  Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..

                  Bob Lehmann wrote:[color=blue][color=green]
                  >> I was expecting a reply more a long the lines of "arrays are more
                  >> costly[/color]
                  >[color=green][color=darkred]
                  >>> )[/color][/color]
                  >
                  > Invariably,[/color]

                  Invariable? :-)
                  [color=blue]
                  > the usual flow with XML is ...
                  >
                  > Load the XML document
                  > Parse and loop through the tags and their children
                  > Place values in an array[/color]

                  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"


                  Comment

                  • Anthony Jones

                    #10
                    Re: [ASP/vbScript] Shopping Cart - removing items by quantity - theory..


                    "Rob Meade" <ku.shn.tsews.t hbu@edaem.bor> wrote in message
                    news:e$Mdpt7jGH A.1324@TK2MSFTN GP04.phx.gbl...[color=blue]
                    > "Anthony Jones" wrote ...
                    >[color=green]
                    > > Have you considered not using arrays but using XML instead. You can
                    > > safely
                    > > store a FreeThreadedDOM Document in the session object.[/color]
                    >
                    > Hi Anthony,
                    >
                    > Thank you for your reply - I have to confess that I've not considered[/color]
                    using[color=blue]
                    > 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[/color]
                    an[color=blue]
                    > 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[/color]
                    not[color=blue]
                    > entirely sure how that would work.
                    >
                    > Any further info is appreciated.
                    >
                    > Regards
                    >
                    > Rob
                    >
                    >[/color]

                    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.CreateOb ject("MSXML2.Fr eeThreadedDOMDo cument.3.0")
                    goOrderDOM.asyn c = False
                    goOrderDOM.load XML "<order />"
                    Set Session("OrderD OM") = goOrderDOM
                    Else
                    Set goOrderDOM = Session("OrderD OM")
                    End If

                    If IsEmpty(Applica tion("ProductDO M")) Then
                    Set goProductDOM =
                    Server.CreateOb ject("MSXML2.Fr eeThreadedDOMDo cument.3.0")
                    goProductDOM.as ync = False
                    goProductDOM.lo ad Server.MapPath( "Products.x ml")
                    Set Application("Pr oductDOM") = goProductDOM
                    Else
                    Set goProductDOM = Application("Pr oductDOM")
                    End If

                    If Request.ServerV ariables("REQUE ST_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.docu mentElement.chi ldNodes
                    %>
                    <tr>
                    <td><%=GetProdu ctName(goLine)% ></td>
                    <td class="quantity "><%=goLine.get Attribute("quan tity")%></td>
                    </tr>
                    <%
                    i = i + 1
                    Next
                    %>
                    <tr>
                    <td>
                    <select name="cboProduc t">
                    <option>- Select Product -</option>
                    <%DrawProductOp tions()%>
                    </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.getAttri bute("productID ")

                    Set oProduct = goProductDOM.se lectSingleNode( "//product[@ID=""" & sProdID &
                    """]")

                    GetProductName = Server.HTMLEnco de(oProduct.Tex t)

                    End Function

                    Sub DrawProductOpti ons

                    Dim oProd

                    For Each oProd in goProductDOM.do cumentElement.c hildNodes
                    Response.Write "<option value=""" & oProd.getAttrib ute("ID") & """>" & _
                    Server.HTMLEnco de(oProd.Text) & "</option>"
                    Next

                    End Sub

                    Sub ProcessPost()

                    Dim sProdID
                    Dim lQty
                    Dim oLine

                    sProdID = Request.Form("c boProduct")
                    lQty = CLng(Request.Fo rm("txtQty"))

                    If sProdID <> Empty Then

                    Set oLine = goOrderDOM.sele ctSingleNode("/order/line[@productID=""" &
                    sProdID & """]")

                    If lQty > 0 Then
                    If oLine Is Nothing Then
                    Set oLine =
                    goOrderDOM.docu mentElement.app endChild(goOrde rDOM.createElem ent("line"))
                    oLine.setAttrib ute "productID" , sProdID
                    End If
                    oLine.setAttrib ute "quantity", lQty
                    Else
                    If Not oLine Is Nothing Then
                    goOrderDOM.docu mentElement.rem oveChild(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">Thingama bobs</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.


                    Comment

                    Working...