7月 16, 2006

Flash取資料庫資料-以Loadvars及XML為例

要以Flash顯示資料庫資料可用LoadvarsXML、Flash Remoting,在這以LoadVars、XML為例,LoadVars和XML運作原理大同小異,送參數觸發伺服器上的程式(ASP、PHP、JSP)連接資料庫。不同點僅在於傳回資料描述的方式不同,Loadvars用"變數=值&變數=值".....變數中間以&符號連結
如:id=6&name1=Michael&address=Coventry House Miner Rd.
而XML當然以XML的方式描述,以下為範例:

Loadvars ASP-engine2.asp
 
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
'隨機選題目並輸出為XML
<!--#include file="connectDB.asp"-->
<%
Response.Expires=-1500
Response.CacheControl="no-cache"
'禁止快取
id=request("id")
set rs=server.CreateObject("ADODB.Recordset")
if id="" then'若沒帶參數過來便亂數取一筆record
sql="select * from Employees ORDER BY NEWID()"
else
'取第三筆record
sql="select * from Employees where EmployeeID="&id
end if
rs.Open sql , conn , 3 , 1
name=rs("FirstName")
response.write "id="&rs("EmployeeID")&"&"
response.write "name1="&rs("FirstName")&"&"
response.write "address="&rs("Address")
rs.close
%>





Flash-a.swf
  
//準備一個按鈕並命名為star_btn三個動態輸入文字框變數設為id、name1、address
//在第一個影格寫上
star_btn.onPress=function(){
var loadasp:LoadVars=new LoadVars();
loadasp.onLoad=myfun;
loadasp.load("http://60.248.36.151/temp/joehwang/engine2.asp");

function myfun(){
trace("load ok");
trace(loadasp.name1);
id=loadasp.id;
name1=loadasp.name1;
address=loadasp.address;
}
}




XML

asp-engine.asp


  
'與ajax連接資料庫範例相同
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%'隨機選題目並輸出為XML%>
<!--#include file="connectDB.asp"-->
<%
Response.Expires=-1500
Response.CacheControl="no-cache"
'禁止快取
id=request("id")
set rs=server.CreateObject("ADODB.Recordset")
if id="" then'若沒帶參數過來便亂數取一筆record
sql="select * from Employees ORDER BY NEWID()"
else
'取第三筆record
sql="select * from Employees where EmployeeID="&id
end if
rs.Open sql , conn , 3 , 1
Response.ContentType="text/xml"'將文件編碼指定為XML格式,asp產生xml格式時必加
response.write "<?xml version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"utf-8"&chr(34)&"?>"
response.write "<areas>"
response.write "<id>"&rs("EmployeeID")&"</id>"
response.write "<name>"&rs("FirstName")&"</name>"
response.write "<address>"&rs("Address")&"</address>"
response.write "</areas>"
rs.close
%>



Flash-xmla.swf
  
//準備一個按鈕並命名為star_btn四個動態輸入文字框變數設為id、name1、address、loading
//在第一個影格寫上
star_btn.onPress=function(){
loading="loading.....";
var myxml:XML=new XML();
myxml.ignoreWhite=true;
myxml.onLoad=myfun;
myxml.load("http://60.248.36.151/temp/joehwang/engine.asp");

function myfun(){
id=myxml.firstChild.childNodes[0].childNodes[0];
name1=myxml.firstChild.childNodes[1].childNodes[0];
address=myxml.firstChild.childNodes[2].childNodes[0];
loading="";
}
}

沒有留言: