By David G.
As we all know, an array is a special type of variable that can be used to represent a list or table of related values. Basically, it is a numbered series of related items. One popular application of an array is utilizing it in an e-commerce shopping cart.
Now, imagine that you are using a session-level two-dimensional array to keep track of products that are added to your web site shopping cart, where each item in the first dimension represents information about the particular product while each item in the second dimension identifies a product in the shopping cart. For example:
Notice that this array kind of looks like a Recordset (a Recordset would have columns describing the properties of
each item, and a row for each item in the shopping cart); a two-dimensional array is just another way
to conceptualize at a Recordset. In fact, the Recordset object contains a
GetRows method, which
transforms a Recordset into a two-dimensional array (for more information read: Using
GetRows to Quickly Display a Skinny Table). However, there is no built-in method to convert an
array into a Recordset.
So, let's examine how we can dump the contents of a two-dimensional array into a Recordset using our shopping cart array example. This script assumes that we already have a two-dimensional array that has been populated with some values. You could simply hard code the creation and population of the array like so:
Now that we have a populated two-dimensional array let's look at how to convert this array into a Recordset.
Note that if you use a connected Recordset, adding these contents to the Recordset will have the effect of adding
these as rows to the database. The code, with comments, is shown below. Note that the script uses some of the
ADO constants, meaning that you'll have to include
adovbs.inc in your ASP pages that use this code (for
more information on
adovbs.inc be sure to read
In actuality, there is more to explain when it comes to developing a workable session-variable based shopping cart application. The above information simply gives a basic and quick introduction on how to send information from a multi-dimensional array into a database table.
And to conclude... that's all there is to it folks! Happy Programming!
|Return to user tips...|