【问题标题】:send an array from php to as3.0将数组从 php 发送到 as3.0
【发布时间】:2016-09-26 05:47:35
【问题描述】:

我正在使用 as3 在 flash cs6 中构建一个 air 应用程序。我需要从 php 发送一个数组到 flash as3.0 我想在我的应用程序上创建一个“时间线”。 我有很多阅读各种参考资料,但没有太大帮助。 这是我使用的代码。 时间线.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:错误 #1010:术语未定义且没有属性。在 函数/MasagiApp_fla:MainTimeline/loadTimeline/MasagiApp_fla:onCompleteLoadTimeline()[MasagiApp_fla.MainTimeline::frame6:52] 在 flash.events::EventDispatcher/dispatchEventFunction() 在 flash.events::EventDispatcher/dispatchEvent() 在 flash.net::URLLoader/onComplete()

请帮帮我

【问题讨论】:

  • 我认为您应该将onCompleteLoadTimeline 函数移到loadTimeline 函数之外。
  • 感谢快速响应,但输出没有变化。

标签: php arrays actionscript-3 flash


【解决方案1】:

不要将数据组装成字符串。在您的情况下,结果不是有效的 url 编码字符串(例如缺少 &),并且它不会像您在代码中所期望的那样自动转换为数组。

JSON 或 XML 是交换数据的更好格式,尤其是当它具有某种结构时。我使用前者来回答这个问题。

说到结构,将属于一起的数据分布在几个数组中是一种糟糕的结构。不要有许多具有单一属性的数组,而是一个数组,其中包含将属性组合在一起的对象。

在您的情况下,一个对象具有属性namepostingID,在 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 是正确的帽子,你不应该将函数相互嵌套!)你 JSON.parse() 接收到的字符串并得到一个通用的 As3 Object 结果:

function onCompleteLoadTimeline(event:Event)
{
    var result:Object = JSON.parse(event.target.data);

    trace (result[0].name);  // should give first name
}

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2014-04-09
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    相关资源
    最近更新 更多