Flash留言本制作详解

来源:互联网 发布:mfc编程书籍 编辑:程序博客网 时间:2024/04/30 01:22

在Flash Professional 8里面,利用ASP与ACCESS数据库,创建属于自己的留言本。

整个留言本的制作分三个部分:

1、数据库部分    2、ASP文件部分    3、Flash文件 部分

1、数据库部分:建立一个Access数据库文件,默认安装了Office软件后就安装了Access。

建立方法:打开Microsoft Office Access 2003,选择“文件-新建-空数据库”,起个名字myData.mdb,点“创建”并保存在 桌面/留言本 文件夹里面。然后点击“通过输入数据创建表”,新建一个留言表。数据字段如下:

|ID|User|IPAddress|wTime|Info|Other,序号、留言人名称,访问IP,留言时间,留言内容,其他,为了简单起见,全部使用文本字段。

2、ASP文件部分:因为Flash不能直接的同数据库进行通讯,所以我们使用ASP与Access进行通讯,把结果发送或返回给Flash文件,通过GET,POST的方法。ASP的文件有2个,一个是读的,一个是写的。文件内容如下:

<%

'读取数据库中的记录
'示例:ASP连接Access数据库,无密码连接

DBPath=Server.MapPath("myData.mdb") '数据库存放路径
Strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & DBPath  '连接字符串
Set conn = Server.CreateObject("ADODB.Connection") '新建一个连接
conn.open Strconn

Set rs=Server.CreateObject("ADODB.RecordSet") '新建一个查询
strSQL="SELECT top 10 * FROM TBL_LYB"  '查询的SQL语句
rs.Open strSQL,conn,1,1  '执行查询

'**********
Dim numRow
numRow = 0
do while not rs.eof
Response.Write "&id"&numRow&"="&rs("ID") & "&user"&numRow&"="&rs("User") & "&ip"&numRow&"="&rs("IPAddress") & "&time"&numRow&"="&rs("wTime") & "&info"&numRow&"="&rs("Info") & "&other"&numRow&"="&rs("Other")
rs.movenext
numRow=numRow+1
loop

Response.Write ("&readResult=OK")
'**********
rs.close
set rs=nothing
conn.close
set conn=nothing

%>

通常在建立ASP 文件的时候,会把连接数据库的代码单独的做一个connect.asp文件,其他文件中使用<!--#include file="connect.asp"-->,我这里并没有这样操作,用的是个笨方法。

<%
'提交留言

Dim user,ip,writetime,info,other
user=Request("user")
info=Request("info")
'other=Request("other")

If ip="" then ip=Request.ServerVariables("REMOTE_ADDR")
writetime=now()

DBPath=Server.MapPath("myData.mdb") '数据库存放路径
Strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & DBPath  '连接字符串
Set conn = Server.CreateObject("ADODB.Connection") '新建一个连接
conn.open Strconn

'strSQL="INSERT INTO TBL_LYB(User,IPAddress,Time,Info,Other) VALUES('"&user&"','"&ip&"','"&time&"','"&info&"','"&other&"')"
strSQL="INSERT INTO TBL_LYB(User,IPAddress,wTime,Info) VALUES('"&user&"','"&ip&"','"&writetime&"','"&info&"')"
conn.execute strSQL

Response.Write ("&writeResult=OK")

conn.close
set conn=nothing

%>

3、最后是Flash的部分了:

这个是第一帧:

// 强迫Flash使用“Flash 5原来编码方法”
System.useCodepage = true;

// 与外部ASP进行数据交换
loadVariables("readly.asp",this,"GET");

// 创建留言表格-列
myDataGrid.addColumn("序号");
myDataGrid.addColumn("留言人");
myDataGrid.addColumn("IP地址");
myDataGrid.addColumn("留言时间");
myDataGrid.addColumn("留言内容");
myDataGrid.addColumn("其他");

// 当从asp文件中获得数据后进行一下处理

function checkParamsLoaded() {
 if(readResult=="OK"){//asp文件处理结束
// 留言列表显示数据,列出最近10条记录
  var myDP_array:Array = new Array();
  myDP_array.push({序号:id0,留言人:user0,IP地址:ip0,留言时间:time0,留言内容:info0,其他:other0});
  myDP_array.push({序号:id1,留言人:user1,IP地址:ip1,留言时间:time1,留言内容:info1,其他:other1});
  myDP_array.push({序号:id2,留言人:user2,IP地址:ip2,留言时间:time2,留言内容:info2,其他:other2});
  myDP_array.push({序号:id3,留言人:user3,IP地址:ip3,留言时间:time3,留言内容:info3,其他:other3});
  myDP_array.push({序号:id4,留言人:user4,IP地址:ip4,留言时间:time4,留言内容:info4,其他:other4});
  myDP_array.push({序号:id5,留言人:user5,IP地址:ip5,留言时间:time5,留言内容:info5,其他:other5});
  myDP_array.push({序号:id6,留言人:user6,IP地址:ip6,留言时间:time6,留言内容:info6,其他:other6});
  myDP_array.push({序号:id7,留言人:user7,IP地址:ip7,留言时间:time7,留言内容:info7,其他:other7});
  myDP_array.push({序号:id8,留言人:user8,IP地址:ip8,留言时间:time8,留言内容:info8,其他:other8});
  myDP_array.push({序号:id9,留言人:user9,IP地址:ip9,留言时间:time9,留言内容:info9,其他:other9});
  myDP_array.push({序号:id10,留言人:user10,IP地址:ip10,留言时间:time10,留言内容:info10,其他:other10});
  myDataGrid.dataProvider = myDP_array;
  clearInterval(param_interval);
  myloading.visible=false;
 }
 else
 {trace("Database connect error!");}
}
// 创建一个监听,每300毫秒运行一次setInterval
var param_interval:Number = setInterval(checkParamsLoaded, 200);

// 与外部ASP进行数据交换
loadVariables("showly.asp",this,"GET");
function checkLoaded() {
 if(readResult=="OK"){
  clearInterval(pint);
  showName.text=user;
  showTime.text=time;
  showIP.text=ip;
  showInfo.text=info;
  showOther.text=other;
 }
 else
 {trace("Database connect error!");}
}
// 创建一个监听,每300毫秒运行一次setInterval
var pint:Number = setInterval(checkLoaded, 200);
var showNum:Number=0;

stop();

当用户准备提交留言的时候处理:

on(click){
 _root.myloading.visible=true;
 // 与外部ASP进行数据交换
 var url:String="writely.asp?user="+_root.liuyanren.text+"&info="+_root.liuyanneirong.text;
 loadVariables(url,"","POST");

 // 提交数据后,根据返回值(判断是否数据写入数据库操作完成)来重新显示
function checkParamsLoaded() {
 if(writeResult=="OK"){
  clearInterval(param_interval);
  _root.gotoAndPlay(1);
 }
 else
 {trace("Database connect error!");}
}
// 创建一个监听,每300毫秒运行一次setInterval
var param_interval:Number = setInterval(checkParamsLoaded, 200);
}