Title: Loading Dynamic Data into Flash
1Loading Dynamic Data into Flash
- Nguy?n Vi?t Thành
- B? môn H? th?ng thông tin Khoa Công ngh? thông
tin - Ð?i h?c Khoa h?c t? nhiên TpHCM
- 22/03/2005
2M?c l?c
- T?ng quan M?c tiêu
- L?y d? li?u t? file text
- L?y d? li?u t? XML
- L?y d? li?u t? CSDL
- L?y d? li?u thông qua Flash Remoting
- So sánh và k?t lu?n
3T?ng quan M?c tiêu
- Gi?i thi?u các cách ph? bi?n dùng d? l?y d? li?u
cho Flash - Th?o lu?n và so sánh các cách này
- K?t lu?n v? các tru?ng h?p s? d?ng cho m?i cách
4Yêu c?u tiên quy?t
- Ph?n m?m so?n th?o Flash Macromedia Flash MX
2004 - Ngôn ng? l?p trình server-side PHP
- Co s? d? li?u MySQL
- Component Action Script dành cho Macromedia Flash
MX - B? cài d?t m? r?ng ph?c v? Action Script dùng cho
PHP AMFPHP
5D? li?u t? file text
- Ð?nh d?ng d? li?u
- keyvalue
- key1value1key2value2key3value3
- key1value1key2value2key1value3key2value4
- VD MSSV9800659TenThanhMSSV9900123TenBinh
- S? d?ng hàm LoadVars() ho?c LoadVariables() d?
l?y d? li?u t? file text - Ví d? d?c m?t dòng d? li?u và nhi?u dòng d? li?u
6D? li?u t? file text VD1
7D? li?u t? file text VD1 (cont)
- N?i dung d? li?u
- TitleAnastasioCommentsThis is a nice
guyImageanastasio.jpg - Source code
- myData new LoadVars()
myData.load("anastasio.txt")
myData.onLoad function(success)
if(success) Title_txt.htmlT
ext ""this.Title"" Comment
s_txt.text this.Comments holder_mc.
loadMovie(this.Image) else
trace("Error loading data")
8D? li?u t? file text VD1 (cont)
9D? li?u t? file text VD2
10D? li?u t? file text VD2 (cont)
- N?i dung d? li?u
- Title0AnastasioComments0This is a nice
guyImage0an1.jpgTitle1Anastasio on
carComments1Here's Anastasio's
carImage1an2.jpgcant2 - Source code
- myData new LoadVars()
- myData.load("anastasio2.txt")
- myData.ref this
- myData.onLoad function(succes) if(succes)
for(var i0 i
this.ref"Title_txt"i.htmlText""thi
s"Title"i"" this.ref"Comments_txt
"i.text this"Comments"i this.ref"h
older_mc"i.loadMovie(this"Image"i)
else trace("Error loading data") -
11D? li?u t? file text VD2 (cont)
12D? li?u t? file XML
- Ð?nh d?ng d? li?u tuân theo chu?n c?a file XML
- Có DTD ho?c schema d? mô t? c?u trúc và ki?u d?
li?u ch?a bên du?i - M?i tag m? s? có m?t tag tuong ?ng d? dóng
- Các tag l?ng bên trong ph?i du?c dóng tru?c các
tag bao bên ngoài - Duy?t tài li?u XML (phân c?p) d? l?y d? li?u c?n
hi?n th? - Ví d?
13D? li?u t? file XML VD
14D? li?u t? file XML VD (cont)
- N?i dung d? li?u
- datatitle name CDATA REQUIRED(PCDATA)itle name"Anastasio"This is a nice
guyan2.jpgtle name"Anastasio's car"Anastasio
loves his caran1.jpgtitle
15D? li?u t? file XML VD (cont)
- Source code
- myXML new XML() myXML.ignoreWhite true
myXML.load("anastasio.xml") myXML.ref this - myXML.onLoad function(succes)
if(succes) var root
this.firstChild nodes
root.childNodes for(var i0
iis.ref"Title_txt"i.text nodesi.attributes.n
ame subnodes
nodesi.childNodes this.ref"Com
ments_txt"i.text subnodes0.firstChild.toStri
ng() this.ref"holder_mc"i.load
Movie(subnodes1.firstChild.toString())
else trace("Error
loading XML document")
16D? li?u t? file XML VD (cont)
17D? li?u t? CSDL
- Ð?nh d?ng d? li?u d? li?u du?c luu tr? trong các
b?ng c?a m?t CSDL quan h? - S? d?ng m?t ngôn ng? server-side (ASP.NET, JSP,
PHP) d? l?y d? li?u t? CSDL và tr? ra text d?nh
d?ng (gi?ng tru?ng h?p l?y d? li?u t? file text) - Ví d? minh h?a dùng PHP và MySQL
18D? li?u t? CSDL VD
19D? li?u t? CSDL VD (cont)
- N?i dung d? li?u
- CREATE TABLE titles (ID int(11) NOT NULL
auto_increment,Title varchar(255) NOT
NULL,Comments mediumtext NOT NULL,Image
varchar(255) NOT NULL,PRIMARY KEY (ID)) - INSERT INTO titles VALUES ( '1', 'Anastasio',
'This is a nice guy', 'an1.jpg') - INSERT INTO titles VALUES ( '2', 'In the car',
'Here's Anastasio's car', 'an2.jpg')
20D? li?u t? CSDL VD (cont)
- Source code server-side
- connect mysql_connect("localhost", "root",
"root") mysql_select_db("anastasio",
connect) - result mysql_query("SELECT Title, Comments,
Image FROM titles") - cant 0
- while(rowmysql_fetch_array(result))
- echo "TitlecantrowTitleCommentscantr
owCommentsImagecantrowImage" - cant
- echo "cantcant"
21D? li?u t? CSDL VD (cont)
- Source code Flash
- myData new LoadVars() myData.ref
this myData.load("anastasio.php")
myData.onLoad function()
if(succes) for(var
i0 iref"Title_txt"i.text this"Title_txt"i
this.ref"Coments_txt"i.tex
t this"Coments_txt"i thi
s.ref"holder_mc"i.loadMovie(this"Image"i)
else
trace("Error loading data")
22D? li?u t? file XML VD (cont)
23D? li?u thông qua Flash Remoting
- Là co ch? c?a Macromedia giúp Flash có th? truy
c?p, th?c hi?n tr?c ti?p các ch?c nang thu?c phía
server. - Không s? d?ng co ch? keyvalue, Flash s? d?ng AMF
(ActionScript Message Format) d? trao d?i d?
li?u. - C?n các b? h? tr? dành riêng cho ASP.NET, Java,
PHP - Ví d? minh h?a dùng AMFPHP
24D? li?u thông qua Flash Remoting VD
25D? li?u t? CSDL VD (cont)
- N?i dung d? li?u
- CREATE TABLE titles (ID int(11) NOT NULL
auto_increment,Title varchar(255) NOT
NULL,Comments mediumtext NOT NULL,Image
varchar(255) NOT NULL,PRIMARY KEY (ID)) - INSERT INTO titles VALUES ( '1', 'Anastasio',
'This is a nice guy', 'an1.jpg') - INSERT INTO titles VALUES ( '2', 'In the car',
'Here's Anastasio's car', 'an2.jpg')
26D? li?u t? CSDL VD (cont)
- Source code server-side
- class News var dbhost
"localhost" var dbname "anastasio"
var dbuser "root" var
dbpass "" function News()
this-methodTable
array( "getTitles" array(
//selects all available Titles "d
escription" "Returns avaible Category of
books", "access"
"remote", // available values are private,
public, remote "returntype"
"recordSet" ) )
// Initialize db connection
this-conn mysql_pconnect(this-dbhos
t, this-dbuser, this-dbpass)
mysql_select_db (this-dbname)
function getTitles()
return mysql_query("SELECT
Title, Comments FROM titles")
27D? li?u t? CSDL VD (cont)
- Source code Flash
- include "NetServices.as
- include "NetDebug.as"
- NetServices.setDefaultGatewayUrl("http//localhos
t/myFolder/gateway.php") - var gatewayConnection
NetServices.createGatewayConnection() - service gatewayConnection.getService("News",
this) - getTitles_Result function(rs) cant
rs.getLength() //how many rows are in our
recordset? for(var i0 i //Dinamyc put base on stage and fetch data to
it attachMovie("base", "base"i, i,
_x200, _y(40i)40) this"base"i.Tit
le_txt.htmlText ""rs.getItemAt(i).
Title"" this"base"i.Comments_txt.
text rs.getItemAt(i).Comments
service.getTitles()
28D? li?u t? file XML VD (cont)
29So sánh và k?t lu?n
- Ch?n cách luu tr? và l?y d? li?u nào cho phù h?p?
- Tùy thu?c vào d? ph?c t?p và nhu c?u c?a ?ng d?ng
- S? lu?ng nhi?u hay ít
- C?u trúc don gi?n hay ph?c t?p
- Tính thay d?i c?p nh?t thu?ng xuyên hay không
- Yêu c?u v? t?c d? c?n truy xu?t nhanh hay ch?m
- Tùy thu?c vào môi tru?ng tri?n khai ?ng d?ng
30Tham kh?o
- Tutorial, Loading data into Flash,
www.flash-db.com, Jorge Solis, 2005 - Tutorial, A Introduction to Flash Remoting,
www.flash-db.com, Jorge Solis, 2005
31Th?o lu?n