Exabytes beginner

Monday, September 15, 2014

Classic ASP: Use multidimensional array to keep the shopping cart item before insert into the database

Basically there are to way to store the shopping cart item before the customers checkout and confirm their selection:

1. Store it to database table
2. Store to multidimensional array

In this article, I am going to discus on how to store the shopping cart items into multidimensional array and store the array elements into the database table. To learn how to create MS Access database please view my tutorial; Create MS Access Database table

Traditionally , the shopping catalog will display the items before the user can choose to buy it.

1. First we  need to connect to the database.

' connect to the database 

dim db_string
db_string="Your_database_string"
cs="Driver={Microsoft Access Driver (*.mdb)};"&_
" DBQ="&db_string&";"
  Set cn = server.createobject("ADODB.Connection")
  cn.mode=3  'adModeReadWrite, to avoid unspecified problem error
  cn.open cs

 If you are not sure the path for the database you can ASP server variable to get the virtual path

response.write server.MapPath("catalog.mdb")


Where "catalog.mdb" is your database name.

2. We assume that in catalog.mdb there is a table name item_tab. Item_tab store products details data to be display in the catalog. If you wish to know on  how to input into database table please refer my tutorial on ASP: Insert into Database

This codes will query from the item_tab table and assign to a multidimensional array


' first select records from item_tab and display it

stritem="select item_id,item_name,item_price,item_qty"
stritem=stritem & " from item_tab where item_qty >='1'"
set rsitem=cn.execute(stritem)

if not rsitem.eof then
' assign the recordset into a multidimensional array
aitem=rsitem.getrows()
end if



<form name="frmpick" action="view_cat_test.asp" method=post ID=Form3>

<table>
<%if isarray(aitem) then%>
<td>
<table>
<th>PICK</th>
<th>ITEM ID</th>
<th>ITEM NAME</th>
<th>PRICE</th>
<th>IN-STOCK</th>
<tr></tr>
<td colspan=5 align=center>
<input type="button" name="btnadd" value="ADD TO YOUR CART" onclick="document.frmpick.submit()" ID=Button1>
<INPUT type="hidden" ID=hstrsel name="hstrsel" value="<%=strsel%>">
</td>
</tr>
<% for i=0 to ubound(aitem,2)%>
<tr>
<td><INPUT type="checkbox" ID=Chksel name="chksel" value="<%=aitem(0,i)%>">

</td>
<%for j=0 to ubound(aitem,1)%>
<td><%=aitem(j,i)%></td>
<%next%>
</tr>
  <%next%>
</table>

</td>


The above codes will display the elements of array in HTML table format.

3. Notice the Lines

<td colspan=5 align=center>

<input type="button" name="btnadd" value="ADD TO YOUR CART" onclick="document.frmpick.submit()" ID=Button1&gt


The Button  is used to send the selected item to the shopping cart

4. Once the button is clicked the selected item will be display in the shopping cart.


<!--Display The shopping Cart -->

<td valign="top" align="center">
<table>
<tr>
<th colspan=2>Shopping Cart</th>
</tr>
<% if isarray(acart) then %>
<tr>
<th>Item</th>
<th>Qty</th>
<th>X</th>
</tr>
<%for n=0 to ubound(acart,2)%>
<tr>
<%for m=0 to ubound(acart,1)%>
<td>
<%=acart(m,n)%>

</td>
<%next %>
<td><INPUT type="checkbox" name="chkrem" id="chkrem" value="<%=acart(0,n)%>"></td>
</tr>
<%next%>
<tr>
<td colspan=2>
<INPUT type="submit" value="Remove" ID=Submit1>
</td>
</tr>
<% else %>
<td>
Your shopping cart is currently empty
</td>
<% end if%>
</table>

</td>
</table>
</form>


The background process for "ADD TO CART " button


'Add into the shopping cart

' get the previous select
strsel=trim(request.Form("hstrsel"))
'response.Write "strsel:" & strsel

' 1. Get the checkbox value (current select)
chksel=trim(request.form("chksel"))
chksel=replace(chksel," ","") ' to remove the extra space in the checkbox value
if chksel<>"" then
if strsel="" then
strsel=chksel
else
strsel=strsel & "," & chksel
end if
end if


5. Remove from the shopping cart. Once the item is selected into the shopping cart, it can be remove using this codes:


' remove item from shopping cart 
chkrem=replace(trim(request.form("chkrem"))," ","")
if chkrem<>"" then
'remove the item from array
arem=split(chkrem,",")
if isarray(arem) then
strsel=strsel&","
for p=0 to ubound(arem)
strsel=replace(strsel,arem(p)&",","")
next
end if
if strsel<>"" then strsel=left(strsel,(len(strsel)-1))
end if 

6. Everytime the user click the checkbox , the system will treat the quantity as 1. It will keep on added to the quantity it the item is clicked again. Example of the string created by the process is something similar like:

strsel= "a,a,b,c,a,d,c,e,f"

Technically after assigning to the multidimensional array the array will be;

Item                Quantity
a                           1
a                           1
b                           1
c                           1
a                           1
d                           1
c                           1
e                           1
f                           1

However, the desired  result to be display in the shopping cart should be

Item                Quantity
a                           3
b                           1
c                           2
d                           1
e                           1
f                           1

I use the dictionary scripting object to handle the repetitive array element and do the counting of the quantity:


if strsel<>"" then  

asel=split(strsel,",")
redim acart(2,0)
Set oDic = CreateObject("Scripting.Dictionary")
For k = 0 To UBound(asel)
If oDic.Exists(asel(k)) Then
oDic.Item(asel(k)) = oDic.Item(asel(k)) +1
Else
oDic.Add asel(k), 1
End If
Next
ecount=0
For Each strDate In oDic.Keys()
'response.Write  "<br>item " & strDate & " --" & oDic.Item(strDate) & " qty"
redim preserve acart(2,ecount)
acart(0,ecount)=strDate
acart(1,ecount)=oDic.Item(strDate)
ecount=ecount+1
Next
end if

To view the live example please click :
Code live example


I AM THE AUTHOR
Noraida Arifin
16 years  programming experience and  still learning.
spent 13 years of my life span as a software engineer
in a multinational company
Currently  a retire programmer.
Feedback are welcome.
Contact me at :
https://www.facebook.com/makcikprogrammer

No comments:

Post a Comment