數據庫字段 表:pro_talbe: id 產品編號 proname 產品名稱 simages 產品小圖 表o_rder: id 訂單編號 p_roid 產品編號 m_um 產品數量 d_time 訂購時間 表co_table: id 客戶編號 nn_ame 客戶姓名 tt_el 聯系電話 ee_m_ail 客戶郵箱 +++++++++++++++++++++++ index.asp源碼 +++++++++++++++++++++++
<%@LANGUAGE="VBSCRIPT"%> <!--#include file="Connections/conn.asp" --> <% set Recordset1 = Server.CreateObject("ADODB.Recordset") Recordset1.ActiveConnection = MM_conn_STRING Recordset1.Source = "SELECT * FROM pro_table" Recordset1.CursorType = 0 Recordset1.CursorLocation = 2 Recordset1.LockType = 3 Recordset1.Open() Recordset1_numRows = 0 %> <% Dim Repeat1__numRows Repeat1__numRows = -1 Dim Repeat1__index Repeat1__index = 0 Recordset1_numRows = Recordset1_numRows + Repeat1__numRows %> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body bgcolor="#FFFFFF" text="#000000"> <table width="583" border="0" cellspacing="0" cellpadding="0"> <% While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF)) %> <tr> <td width="140"><%=(Recordset1.Fields.Item("id").Value)%></td> <td width="171"><%=(Recordset1.Fields.Item("proname").Value)%></td> <td width="272"><%=(Recordset1.Fields.Item("simages").Value)%></td> <td width="272"><a href="cart.asp?ProductID=<%=(Recordset1.Fields.Item("id").Value)%>&quantity=1">購買</a></td> </tr> <% Repeat1__index=Repeat1__index+1 Repeat1__numRows=Repeat1__numRows-1 Recordset1.MoveNext() Wend %> </table> </body> </html> <% Recordset1.Close() %> 列出產品,這里有一個要點就是:<a href="cart.asp?ProductID=<%=(Recordset1.Fields.Item("id").Value)%>&quantity=1">購買</a> 這里傳遞了兩個參數:產品ID“ProductID" 數量:“quantity"
+++++++++++++++++++++++++ 下面是cart.asp的源碼: +++++++++++++++++++++
<%@LANGUAGE="VBSCRIPT"%> <!--#include file="Connections/conn.asp" --> <% if not IsObject(session("cart")) then Set session("cart")=CreateObject("Scripting.Dictionary") end if Set cart=session("cart") productID=Cstr(request("productID")) dim detail(1) detail(0)=Int(request("quantity")) if request("productID")<>"" then if cart.Exists(productID) then cart.Remove(productID) end if cart.add productID,detail end if keys=cart.keys items=cart.items set session("cart")=cart %>
<HTML> <HEAD> <TITLE>浙江廣源印刷包裝有限公司</TITLE> <META http-equiv=Content-Type content="text/html; charset=gb2312"> <LINK href="index/index.css" type=text/css rel=stylesheet> <META content="MSHTML 5.50.4807.2300" name=GENERATOR> </HEAD> <BODY text=#000000 bgColor=#ffffff leftMargin=0 topMargin=0 marginwidth="0" marginheight="0"> <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF"> <tr> <td width="100%" valign="top"> <br> <table border="2" width="100%" cellspacing="0" cellpadding="0" bordercolorlight="#FFFFFF" bordercolordark="#FFFFFF" bgcolor="#D00000" height="1" align="center"> <tr bgcolor="#3399FF"> <td width="25%" align="center" height="19"><font color="#FFFFFF">商品名稱</font></td> <td width="25%" align="center" height="19"><font color="#FFFFFF">購買數量</font></td> <td width="12%" align="center" height="19"><font color="#FFFFFF">更新</font></td> <td width="13%" align="center" height="19"><font color="#FFFFFF">刪除</font></td> </tr> <% For i = 0 To cart.Count -1 %> <% Dim list__MMColParam list__MMColParam = keys(i) %> <% set list = Server.CreateObject("ADODB.Recordset") list.ActiveConnection = MM_conn_STRING list.Source = "SELECT * FROM pro_table WHERE id = " + Replace(list__MMColParam, "’", "’’") + "" list.CursorType = 0 list.CursorLocation = 2 list.LockType = 3 list.Open() %> <form method="post" action="cart.asp" onSubmit="return checkform(this);"> ’更新產品數量用 <tr bgcolor="#ECECFF"> <td width="25%" align="center" height="1"><a href="detail.asp?productID=<%=keys(i)%>"><font color="#000077" ></font></a><%=(list.Fields.Item("proname").Value)%></td> <td width="25%" align="center" height="1"> <input name="quantity" size=4 class="edit" value="<%=items(i)(0)%>"> <input type="hidden" name="productID" value="<%=keys(i)%>"> </td> <td width="12%" align="center" height="1"> <input type="image" border="0" name="imageField" src="http://edu.chinaz.com/Get/Program/Asp/images/ref.gif" width="17" height="19"> </td> <td width="13%" align="center" height="1"><a href="delcart.asp?productID=<%=keys(i)%>"><img src="images/del.gif" width="17" height="19" border="0"></a></td> </tr> </form> <% list.Close() %> <% next %> <tr > <td width="75%" bgcolor="#000000" align="center" height="1" colspan="2"> <p align="right"><font color="#FFFFFF">總價合計:</font> </td> <td width="25%" bgcolor="#000000" align="center" height="1" colspan="2"><font color="#FFFFFF">¥<%=price%></font></td> </tr> </table> <p align="center"><b><a href="index.asp">繼續選購</a> <a href="order.asp">付款</a></b> </td> </tr> </table> </BODY> </HTML>
[page_break]其中最前面代碼,下面的理解是我自已想的有可能有錯,不過程序絕對沒有錯:<% if not IsObject(session("cart")) then Set session("cart")=CreateObject("Scripting.Dictionary")’建立二維數組 end if Set cart=session("cart") productID=Cstr(request("productID"))’定義變量productid為產品頁傳遞過來的值 dim detail(1) detail(0)=Int(request("quantity")) if request("productID")<>"" then if cart.Exists(productID) then cart.Remove(productID) end if cart.add productID,detail ’上面這一段我看不太懂,誰知道告訴我一下,不過和下面的有關 end if keys=cart.keys ’定義變量KEYS的值為數組里面的產品ID items=cart.items ’定義變量items的值為數組里面的產品數量 set session("cart")=cart %> 代碼二: <% For i = 0 To cart.Count -1 %> ’設置多個產品的循環,cart.count是指購買幾個產品,后面要以next結束 <% Dim list__MMColParam list__MMColParam = keys(i) %> ’定義ID為cart數組里面的產品ID號 <% set list = Server.CreateObject("ADODB.Recordset") list.ActiveConnection = MM_conn_STRING list.Source = "SELECT * FROM pro_table WHERE id = " + Replace(list__MMColParam, "’", "’’") + "" list.CursorType = 0 list.CursorLocation = 2 list.LockType = 3 list.Open() %> ’上面的代碼就不用我說了,其中要注意的:list__MMColParam = keys(i)
………………………………………… <% list.Close() %> <% next %>
++++++++++++++++++++++ 取消訂購的產品:delcart.asp ++++++++++++++++++++++
<% Set cart=session("cart") if request("productID")<>"" then productID=Cstr(request("productID")) cart.Remove(productID) end if set session("cart")=cart response.redirect("cart.asp") %>
+++++++++++++++++++++++++ order.asp源碼,這里很簡單,看一下就明白了 +++++++++++++++++++++++
<HTML><HEAD> <TITLE>浙江廣源印刷包裝有限公司</TITLE> <META http-equiv=Content-Type content="text/html; charset=gb2312"> <LINK href="index/index.css" type=text/css rel=stylesheet> <META content="MSHTML 5.50.4807.2300" name=GENERATOR> </HEAD> <BODY text=#000000 bgColor=#ffffff leftMargin=0 topMargin=0 marginwidth="0" marginheight="0"> <div align="center"> <form name="form2" method="post" action="sendorder.asp" onSubmit="return check1()"> <table width="514" border="0" cellspacing="0" cellpadding="0" height="15" align="center"> <tr> <td height="6" bgcolor="#FF9900"> <font color="#FFFFFF" size="2"> 購買者聯系信息:</font></td> </tr> </table> <table border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td> <div align="center"><font size="2">聯系人</font></div> </td> <td> <div align="left"><font size="2"> <input type="text" name="n_ame" size="20"> </font></div> </td> </tr> <tr> <td> <div align="center"><font size="2">公司電話</font></div> </td> <td> <div align="left"><font size="2"> <input type="text" name="t_el" size="20"> </font></div> </td> </tr> <tr> <td> <div align="center"><font size="2">電子郵箱</font></div> </td> <td> <div align="left"><font size="2"> <input type="text" name="e_mail" size="20"> </font></div> </td> </tr> <td> <div align="center"></div> </td> <td> </td> </tr> </table> <p align="center"> <input name="submit" type="submit" value="提交"> <input name="reset" type="reset" value="清除"> </p> </form> </div> </BODY></HTML> ++++++++++++++++++++++++++++++++ sendorder.asp,提交選購的產品到數據庫, +++++++++++++++++++++++++++++
<%@LANGUAGE="VBSCRIPT"%> <!--#include file="Connections/conn.asp" -->
<% if not IsObject(session("cart")) then response.redirect("/") end if %> ’加一個判斷,如果session("cart")為空,則重定位URL <% Set rs1=server.createobject("ADODB.recordset") Set cart=session("cart") keys=cart.keys ’取出數組里面的產品ID items=cart.items ’取出數組里面的產品數量 %> <%
set list = Server.CreateObject("ADODB.Command") list.ActiveConnection = MM_conn_STRING For i = 0 To cart.Count -1 list.CommandText = "INSERT INTO o_rder (m_um, p_roid) VALUES (" + Replace(items(i)(0), "’", "’’") + "," + Replace(keys(i), "’", "’’") + " ) "
list.CommandType = 1 list.CommandTimeout = 0 list.Prepared = true list.Execute() next %> <%
set add = Server.CreateObject("ADODB.Command") add.ActiveConnection = MM_conn_STRING add.CommandText = "INSERT INTO co_table (nn_ame, tt_el, ee_m_ail) VALUES (" + Replace(Request.form("n_ame"), "’", "’’") + ", " + Replace(Request.form("t_el"), "’", "’’") + ", " + Replace(Request.form("e_mail"), "’", "’’") + ") " add.CommandType = 1 add.CommandTimeout = 0 add.Prepared = true add.Execute()
%> <html> <head> <title>浙江廣源印刷包裝公司</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link rel="stylesheet" href="style.css" type="text/css"> </head> <body text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" > <p><%= Request.form("n_ame") %>你好,以下是你的詳單</p> <p><%= Request.form("n_ame") %></p> <p><%= Request.form("t_el") %></p> <p><%= Request.form("e_mail") %></p> <p> <%For i = 0 To cart.Count -1%> </p> <p> id:<%=keys(i)%><br> 數量:<%=items(i)(0)%><br><%next%> </p> <p> </p> <p> </p> <p> </p> </body> </html> <% session("cart")=""%>
上面用了兩個命令預存過程: 第一個過程:list 存儲產品到數據庫 因為有可能選購多個產品,所以用了一個循環For i = 0 To cart.Count -1,并以next結束
第二個過程:add 存儲客戶到數據庫 就這樣一個簡單的購物車做好了。
本教程中主要的是cart.asp中的定義一個二維數組,還有用循環For i = 0 To cart.Count -1 next 把所訂購的產品顯示出來
|