这里就不多说了,应该很轻易就可以看懂了!这里没有用TextArea而改用了DataGrid! :-) 刚学会!
import mx.remoting.Service;
import mx.services.Log;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.debug.NetDebug;
import mx.controls.Alert; //加入Alert,以便弹出一个提示信息
//Initialize a Logger
mx.remoting.debug.NetDebug.initialize(); // initialize the NCD
var myLogger:Log = new Log( Log.DEBUG, "logger1" );
// override the default log handler
myLogger.onLog = function( message:String ):Void {
trace( "myLogger-->>>"+message );
}
//设定全局Service
_global.svc = new Service(
"http://localhost/amfphp/gateway.php",
myLogger,
"favorite",
null,
null);
/**
* 设定添加按钮的动作
*/
append_btn.onRelease = function(){
var pc:PendingCall = svc.append(webname_txt.text,url_txt.text); //呼叫php中的append方法
pc.responder = new RelayResponder(this, "append_Result", "onError");
webname_txt.text = url_txt.text = ""; //清空文本框
}
append_Result = function(re:ResultEvent){ //添加按钮响应结果
var id = re.result;
if(id != "error"){
mx.controls.Alert.show ("保存:"+id+" ->成功", "Debug Message", Alert.YES, _root, false, "prueba", Alert.OK);
refresh_data(); //刷新数据
}
else {
mx.controls.Alert.show ("技术原因未成保存", "Debug Message", Alert.YES, _root, false, "prueba", Alert.OK);
}
}
/***
* 刷新按钮
*/
refresh_btn.onRelease = function(){
favorite_dg.removeAllColumns();
refresh_data();
}
/**
* 刷新数据
*/
function refresh_data(){
var pc:PendingCall = svc.get_data();
pc.responder = new RelayResponder(this, "getData_Result", "onError" );
}
/***
* 接收数据显示
*/
function getData_Result(re: ResultEvent){
var rs = re.result;
favorite_dg.columnNames = ["id_PK", "webname", "url", "timeline"];
favorite_dg.dataProvider = rs;
/* 遍历方法
f = rs.length; //数库集大小
for(var i=0; i<f; i++){
favorite_dg.addItem({id_PK:rs.getItemAt(i).id_PK, webname:rs.getItemAt(i).webname, url:rs.getItemAt(i).url, timeline:rs.getItemAt(i).timeline});
}
*/
}
/***
* 出错 :-(
*/
function onError(rs: FaultEvent){
mx.remoting.debug.NetDebug.trace({level:"None", message:"There was a problem: " + fault.fault.faultstring });
}
refresh_data(); //初始数据
stop();
php的服务
<?php
/***
* php & flash 收藏夹 之amfphp实现+MySql
*
* 这里要实现的功能是,用flash来展示和通过php来读写文本来
* 实现一个收藏夹的功能
*
* 数据的储存结构
* id_PK,webname,url,timeline
*/
class favorite
{
var $dbhost = "localhost"; //数据库地址
var $dbname = "favorite"; //数据库名称
var $dbuser = "root"; //数据库用户名
var $dbpass = ""; //数据库密码
var $conn; //数据库连
function favorite(){
$this->methodTable = array(
"get_data" => array(
"description" => "Returns data",
"access" => "remote",
"returntype" => "recordSet" //返回数据为数据集(上例为字符串)
),
"append" => array(
"description" => "Inserts a Data",
"access" => "remote",
"arguments" => array ("name", "url"), // 需要两个参数
"returntype" => "String"
),
);
//连接数据库
$this->conn = mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpass);
//$this->conn = mysql_pconnect($this->dbhost, $this->dbuser, $this->dbpass);
mysql_select_db ($this->dbname);
}
/**
* 实现添加的功能
* @ $name : 收藏的网站名
* @ $url : 网址
*/
function append($name,$url){
$rs = mysql_query("INSERT INTO tbl_favorite(webname,url) VALUES(’".$name."’, ’".$url."’)");
if(mysql_error()) return "error";
else return $name;
}
/***
* 实现读取功能
* 返回:数组
*/
function get_data(){
return mysql_query("SELECT * FROM tbl_favorite");
}
}
//debug
//$f = new favorite;
//print_r($f->get_data());
//$f->append("korptons blog","http://www.oiasoft.com");
?>
少了一个mysql:
-- phpMyAdmin SQL Dump
-- version 2.8.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2007 年 04 月 08 日 13:04
-- 服务器版本: 5.0.19
-- PHP 版本: 5.0.4
--
-- 数据库: `favorite`
--
-- --------------------------------------------------------
--
-- 表的结构 `tbl_favorite`
--
CREATE TABLE `tbl_favorite` (
`id_PK` int(11) unsigned NOT NULL auto_increment,
`webname` varchar(20) NOT NULL,
`url` varchar(100) NOT NULL,
`timeline` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id_PK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- 导出表中的数据 `tbl_favorite`
--
INSERT INTO `tbl_favorite` (`id_PK`, `webname`, `url`, `timeline`) VALUES (1, ’korptons blog’, ’http://www.oiasoft.com/’, ’2007-04-08 12:21:17’),
(2, ’yahoo’, ’http://cn.yahoo.com’, ’2007-04-08 12:12:33’),
(3, ’kk’, ’http://ww’, ’2007-04-08 12:45:33’),
(4, ’kfc’, ’http://www.kfc.com.cn’, ’2007-04-08 12:46:52’);
附件: tutor_favorite3.rar