開發(fā)Flash留言本一直都是Flash后臺交互的進(jìn)階技術(shù),本例通過ASP和XML簡單高速地同數(shù)據(jù)庫交互。
本例思路:
<1> 創(chuàng)建數(shù)據(jù)庫。
<2> 編寫ASP程序。
<3> 在Flash中制作留言本皮膚。
<4> 編寫AS與ASP程序交互。
實(shí)例步驟:
(1)先安裝IIS服務(wù)器,安裝方法請查閱相關(guān)資料。
(2)交互流程:
顯示:使用ASP讀取數(shù)據(jù)庫數(shù)據(jù)再輸出成XML,F(xiàn)lash再去讀取這個XML文件。
留言:Flash將用戶輸入留言內(nèi)容發(fā)送給ASP,ASP接收到數(shù)據(jù)后再存入數(shù)據(jù)庫。
交互流程如圖所示。
圖14-1 流程圖
(3)本范例使用到的三個ASP文件為:
“head/conn.asp”打開數(shù)據(jù)庫。
“listXML.asp”讀取ACCESS數(shù)據(jù)庫里的數(shù)據(jù)并轉(zhuǎn)化為XML格式顯示出來。
“post.asp”接收Flash里的值存入數(shù)據(jù)庫。
(4)先創(chuàng)建數(shù)據(jù)庫名為“db.mdb”,新建一個表名為“book”,各字段與作用如圖14- 2所示.
圖14-2 新建表
(5)先編寫打到數(shù)據(jù)庫的conn.asp文件。
ASP/Visual Basic代碼
<% Db = "head/db.mdb" conn = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(db) %> |
(6)編寫顯示留言列表的ASP文件listXML.asp,先打開表“book”。
ASP/Visual Basic代碼
<%@LANGUAGE="VBSCRIPT" %> <!--#include file="head/conn.asp" --> <% set rs = Server.CreateObject("ADODB.Recordset") rs.ActiveConnection = conn rs.Source = "SELECT * FROM book ORDER BY id DESC" rs.CursorType = 0 rs.CursorLocation = 3 rs.LockType = 1 rs.Open() %> |
(7)在Flash中讀取數(shù)據(jù)庫數(shù)據(jù)時,還要考慮分頁的問題,我們每次默認(rèn)讀取10條數(shù)據(jù)可以在調(diào)用時設(shè)置顯示條數(shù),使用變量“itemNum”。并判斷當(dāng)前顯示的頁碼,使用變量“pageNum”。
ASP/Visual Basic代碼
<% Dim itemNum,pageNum,recordTotal itemNum = Request.QueryString("itemNum") If itemNum <> "" Then itemNum = Int(itemNum) else itemNum =10 end if recordTotal = rs.RecordCount pageNum = Request.QueryString("pageNum") If pageNum <> "" Then pageNum = Int(pageNum) rs.move(pageNum*itemNum) end if %> |
(8)將數(shù)據(jù)打印成XML文件現(xiàn)顯出來。
ASP/Visual Basic代碼
<?xml version='1.0' encoding='gb2312'?> <XML total='<%=(recordTotal)%>'> <% While ((itemNum <> 0) AND (NOT rs.EOF)) %> <% id=rs("id") myname=Trim(rs("name")) mytitle=Trim(rs("title")) msg=Trim(rs("msg")) qq=Trim(rs("qq")) redate=Trim(rs("date")) %> <item id="<%=id%>" name="<%=myname%>" title="<%=mytitle%>" msg="<%=msg%>" qq="<%=qq%>" date="<%=redate%>"/> <% itemNum=itemNum-1 if NOT rs.EOF then rs.MoveNext() end if Wend %> </XML> |
技術(shù)看板:代碼解釋
第27行,標(biāo)識XML版本與語言類型。
第28行,使用屬性total記錄數(shù)據(jù)庫所有數(shù)據(jù)。
第40行,輸出XML元素,每條記錄著一條用戶留言。