将数组从 php 发送到 as3.0
send an array from php to as3.0
我正在使用 as3 在 flash cs6 中构建一个 air 应用程序。我需要从 php 发送一个数组到 flash as3.0
我想在我的应用程序上创建一个 "timeline"。
我有很多阅读各种参考资料,但帮助不大。
这是我使用的代码。
timeline.php 文件
require_once "connect.php";
$action = isset($_GET['action'])?$_GET['action']:'';
$body_nama = array();
$body_postingan = array();
$total_likers = array();
$id = array();
switch($action){
case 'posting':
posting();
break;
case 'like':
like();
break;
case 'delet_ini':
deletIni();
break;
case 'load_timeline':
loadTimeline();
break;
case 'load_timeline_lama':
loadTimelineLama();
break;
}
function loadTimeline(){
global $body_nama;
global $body_postingan;
global $total_likers;
global $id;
$query_total = "SELECT COUNT(*) FROM timeline_posts";
$result_total = mysql_query($query_total);
$total = mysql_result($result_total,0);
for ($i =0; $i<=9; $i++){
$query_timline = "SELECT * FROM timeline_posts WHERE id = ('$total'-'$i')";
$result = mysql_query($query_timline);
while ($data = mysql_fetch_array($result)){
$body_nama[$i] = htmlentities($data['timeline_name']);
$body_postingan[$i] = htmlentities($data['timeline_post']);
$id[$i] = htmlentities($data['id']);
print "nama[$i]=$body_nama[$i]";
print "postingan[$i]=$body_postingan[$i]";
print "id[$i]=$id[$i]";
}
}
}
这里是as3.0代码
function loadTimeline(){
var phpFileRequest:URLRequest = new URLRequest("http://localhost/social_media_1/timeline.php?action=load_timeline");
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, onCompleteLoadTimeline);
phpLoader.load(phpFileRequest);
function onCompleteLoadTimeline(event:Event){
trace (event.target.data.nama[0]);
trace (event.target.data.postingan[0]);
trace (event.target.data.id[0]);
}
}
但是我有错误。
TypeError: Error #1010: A term is undefined and has no properties. at
Function/MasagiApp_fla:MainTimeline/loadTimeline/MasagiApp_fla:onCompleteLoadTimeline()[MasagiApp_fla.MainTimeline::frame6:52]
at flash.events::EventDispatcher/dispatchEventFunction() at
flash.events::EventDispatcher/dispatchEvent() at
flash.net::URLLoader/onComplete()
请帮帮我
不要像您那样assemble将您的数据转换成字符串。在您的情况下,结果不是有效的 url 编码字符串(例如缺少 &),并且它不会像您在代码中期望的那样自动转换为数组。
JSON 或 XML 是交换数据的更好格式,尤其是当它具有某种结构时。我用前者来回答这个问题。
说到结构,将属于同一数据的数据分布在多个数组中是一种不好的结构。不要有许多具有单一属性的数组,而是一个数组,其中包含将属性组合在一起的对象。
在您的例子中,一个对象具有属性 name
、posting
和 ID
,在 JSON 中看起来像这样:
{
"name":"foo",
"posting":"bar",
"ID":"123"
}
因为你有很多这样的对象,所以它们在一个数组中:
[
{
"name":"foo",
"posting":"bar",
"ID":"123"
},
{
"name":"foo000oo",
"posting":"baAAaar",
"ID":"456"
}
]
这是来自您的服务器的响应正文的样子。
在 PHP 中,您有 mysqli_fetch_assoc()
和 json_encode()
将您的数据从数据库中获取到 JSON 表示 as you can see in this other answer
在客户端(是的,@Jonny Henly is right hat you should not nest functions into each other!) you JSON.parse() 收到的字符串并得到一个通用的 As3 Object
作为结果:
function onCompleteLoadTimeline(event:Event)
{
var result:Object = JSON.parse(event.target.data);
trace (result[0].name); // should give first name
}
我正在使用 as3 在 flash cs6 中构建一个 air 应用程序。我需要从 php 发送一个数组到 flash as3.0 我想在我的应用程序上创建一个 "timeline"。 我有很多阅读各种参考资料,但帮助不大。 这是我使用的代码。 timeline.php 文件
require_once "connect.php";
$action = isset($_GET['action'])?$_GET['action']:'';
$body_nama = array();
$body_postingan = array();
$total_likers = array();
$id = array();
switch($action){
case 'posting':
posting();
break;
case 'like':
like();
break;
case 'delet_ini':
deletIni();
break;
case 'load_timeline':
loadTimeline();
break;
case 'load_timeline_lama':
loadTimelineLama();
break;
}
function loadTimeline(){
global $body_nama;
global $body_postingan;
global $total_likers;
global $id;
$query_total = "SELECT COUNT(*) FROM timeline_posts";
$result_total = mysql_query($query_total);
$total = mysql_result($result_total,0);
for ($i =0; $i<=9; $i++){
$query_timline = "SELECT * FROM timeline_posts WHERE id = ('$total'-'$i')";
$result = mysql_query($query_timline);
while ($data = mysql_fetch_array($result)){
$body_nama[$i] = htmlentities($data['timeline_name']);
$body_postingan[$i] = htmlentities($data['timeline_post']);
$id[$i] = htmlentities($data['id']);
print "nama[$i]=$body_nama[$i]";
print "postingan[$i]=$body_postingan[$i]";
print "id[$i]=$id[$i]";
}
}
}
这里是as3.0代码
function loadTimeline(){
var phpFileRequest:URLRequest = new URLRequest("http://localhost/social_media_1/timeline.php?action=load_timeline");
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, onCompleteLoadTimeline);
phpLoader.load(phpFileRequest);
function onCompleteLoadTimeline(event:Event){
trace (event.target.data.nama[0]);
trace (event.target.data.postingan[0]);
trace (event.target.data.id[0]);
}
}
但是我有错误。
TypeError: Error #1010: A term is undefined and has no properties. at Function/MasagiApp_fla:MainTimeline/loadTimeline/MasagiApp_fla:onCompleteLoadTimeline()[MasagiApp_fla.MainTimeline::frame6:52] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()
请帮帮我
不要像您那样assemble将您的数据转换成字符串。在您的情况下,结果不是有效的 url 编码字符串(例如缺少 &),并且它不会像您在代码中期望的那样自动转换为数组。
JSON 或 XML 是交换数据的更好格式,尤其是当它具有某种结构时。我用前者来回答这个问题。
说到结构,将属于同一数据的数据分布在多个数组中是一种不好的结构。不要有许多具有单一属性的数组,而是一个数组,其中包含将属性组合在一起的对象。
在您的例子中,一个对象具有属性 name
、posting
和 ID
,在 JSON 中看起来像这样:
{
"name":"foo",
"posting":"bar",
"ID":"123"
}
因为你有很多这样的对象,所以它们在一个数组中:
[
{
"name":"foo",
"posting":"bar",
"ID":"123"
},
{
"name":"foo000oo",
"posting":"baAAaar",
"ID":"456"
}
]
这是来自您的服务器的响应正文的样子。
在 PHP 中,您有 mysqli_fetch_assoc()
和 json_encode()
将您的数据从数据库中获取到 JSON 表示 as you can see in this other answer
在客户端(是的,@Jonny Henly is right hat you should not nest functions into each other!) you JSON.parse() 收到的字符串并得到一个通用的 As3 Object
作为结果:
function onCompleteLoadTimeline(event:Event)
{
var result:Object = JSON.parse(event.target.data);
trace (result[0].name); // should give first name
}