session| Shopping Cart | I'm going to include all the programs I've used in a VBScript class. It consists of 3 functions: AddItem (), DeleteItem () and DeleteAll (), and a public process: Displaycart (). The first two functions additem () and DeleteItem () have only one parameter, which is the item to be added or deleted, which can be a commodity name (a string) or a product ID number corresponding to the database. The purpose of the DeleteAll () function is to empty the shopping cart. The parameter of the procedure Displaycart () can be "list" or "option", which determines whether to display the cart information in the form of a list or a drop-down box.
This VBScript class also includes two private functions Readfromdb () and Regexpitem () for AddItem () and DeleteItem () calls.
It is more convenient to use the commodity ID number than the product name, so we use the commodity ID number as the parameter in the AddItem () or DeleteItem () function, in which we pass the ID number to the Readfromdb () function, by Readfromdb () function to read the product name from the database. Since Readfromdb () is not intended to be invoked in other functions, we declare readfromdb () as a private function. Similarly, the following Regexpitem () is also a private function.
The Readfromdb () function
Now let's look at the Readfromdb () function:
Private Function Readfromdb (Argitem)
Dim objconn, objRS
Dim strSQL
Strsql= "SELECT ProductName from tblproducts WHERE productid=" &argitem
Set Objconn=server.createobject ("Adodb.connection")
objConn.Open "File name=d:\inetpub\wwwroot\shoppingcarts\shoppingcarts.udl"
Set Objrs=objconn.execute (strSQL)
Readfromdb=objrs ("ProductName")
Objrs.close
Set objrs=nothing
Objconn.close
Set objconn=nothing
End Function
This function first establishes a database connection, then reads the database, and finally returns the name of the commodity corresponding to the parameter (commodity ID number). This article comes with an Access database containing 10 items for your reference.