flash後台資料連線(PHP篇)之php&flash收藏夾---amfphp實現+mysql
來源:互聯網
上載者:User
mysql|後台|資料 這裡就不多說了,應該很容易就可以看懂了!這裡沒有用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’);