子阵列消失在Amfphp,Actionscript 3数据集成中

时间:2017-04-12 22:47:39

标签: php mysql actionscript-3 amfphp

我最近升级到php 5.6和amfphp 2.2.2,我试图在几年前写的一个Actionscipt 3动画中修改数据的处理方式(我还没有和AS3合作过)很长一段时间,这可能是问题的一部分)。

amfphp网站(Silex Labs)表示只需将PDO用于您的数据和AS3,amfphp将正确处理它,但这不起作用。

AS3"响应者" class处理来自amfphp的传入数据,它不再使用" responds.serverInfo.initialData"。因此,AS3不再像使用amfphp 1.9那样处理数据。

所以我写了一个脚本来解析数据。我显然不了解amfphp为AS3提供的数据对象的结构,但我能够从对象中提取数据并存储在数组中。

我可以在AS3脚本中跟踪三重循环中的数据,但在循环之外,子数组是未定义的。

我必须在AS3中做错事。谁能看到它是什么?

php:

<?php
function getConnection() {
    return new PDO('mysql:host=localhost;dbname=db-name', 'root', '',
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}

function getData() {
    $pdo = getConnection();
    $tsql = "SELECT col0, col1, col2, col3, col4, col5, col6, col7 
           FROM somedata ORDER BY index_num ASC"; 

    $stmt = $pdo->prepare($tsql);
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $results;
}

&GT;

AS3:

        public class GetSomeData extends Sprite {
    private var gw:NetConnection = new NetConnection();
    private var getRes:Responder = new Responder(onResult, onFault);
    private var someData:Array = new Array();
    private var colList:Array = new Array();

    public function GetSomeData() {}

    public function refreshSomeData():void {
        gw.connect("http://localhost/Amfphp/");         
        gw.call("GetSomeData.getData", getRes);
    }


    private function onResult(responds:Object):void {
        /****** THIS IS OUTDATED AS OF amfphp 2.x *******/
        //responds is an arrayCollection which includes .serverInfo.initialData
        //someData = responds.serverInfo.initialData;

        //this is because amfphp returns the columns in the SELECT statement out of order
        colList = ["col0", "col1", "col2", "col3", "col4", "col5", "col6", "col7"];

        for (var j:uint=0; j < colList.length; j++) {
            for (var k:uint=0; k < responds.length; k++) {
                    for(var x in responds[k]){//put columns in correct order
                        if (x.toString() == colList[j]){
                            someData[k] = new Array();
                            someData[k][j] = responds[k][x];
                            //this trace reports all of the data correctly
                            trace(someData[k][j].length);//
                        }
                    }
           }
        }

        trace(someData.length);//49
        trace(someData[0].length);//8
        trace(someData[0][0].length);//undefined

    }
}

1 个答案:

答案 0 :(得分:0)

我能够让我的所有AS3动画都能正常工作,我希望与Amfphp 2.2.2和AS3分享我发现的使用PHP 5.6.2的内容。我认为我的AS3代码看起来过头了,但经过3天的努力,这是我能想到的最好的,而且它有效。

我的主要目的是让数据数组的结构恢复到我使用php 5.2,amfphp 1.9和mysql_query()时的状态。

Amfphp在GetSomeData.php中调用getData()函数,它显然使用了它的SELECT语句。但Amfphp以无序状态而不是SELECT语句中指定的顺序将数据返回到Actionscript。这种无序状态不是按字母顺序列名列,也不是先按最小字段大小排序。它似乎是随机的。

但是,Amfphp确实以SELECT顺序返回getColumns()中的SELECTed列名。所以我使用getColumns()(在Actionscript中)的结果将getData()结果按照我需要的顺序放置。

这是现在正在运行的PHP(是的,我知道没有错误检查,必须在以后进行):

<?php
    class GetSomeData{
        public static function getConnection() {
            return new PDO('mysql:host=localhost;dbname=exampleDB', 'root', ''
        }

        function getColumns() {
            $pdo = getConnection();
            $sql = "SELECT COLUMN_NAME 
                    FROM INFORMATION_SCHEMA.COLUMNS 
                    WHERE TABLE_SCHEMA='exampleDB' 
                    AND TABLE_NAME='sometable'"; 

            $stmt = $pdo->prepare($sql);
            $stmt->execute();
            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $results;
        }

        function getData() {
            $pdo = getConnection();
            $tsql = "SELECT col1, col2, col3, col4, col5, col6, col7
                     FROM sometable"; 
            $stmt = $pdo->prepare($tsql);
            $stmt->execute();
            $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $results;
        }
    }
?>

如果从此脚本中删除“class GetSomeData”和“public static”并在getColumns()中使用print_r($ results)并从网页运行它,您将看到SELECT顺序中的列: 数组([0] =&gt;数组([col1] =&gt; 1.01 [col2] =&gt; 1.23 [col3] =&gt; 2.23 [col4] =&gt; 1.31 [col5] =&gt; 2.44 [col6] =&gt; ; 1.33 [col7] =&gt; 1.76))

这是AS3:

package { 
    import flash.display.Sprite; 
    import flash.events.*; 
    import flash.net.*;

    public class GetStrokeData extends Sprite {
        private var gw:NetConnection = new NetConnection();
        private var getRes:Responder = new Responder(onResult, onFault);

        private var someStrings:Array = new Array();
        private var someData:Array = new Array();
        private var colNames:Array = new Array();
        private var arraysLoaded:Boolean = false;

        public function GetSomeData() {}

        public function refreshData():void {
            if(colNames[0] == undefined){
                gw.connect("/Amfphp/");         
                gw.call("GetSomeData.getColumns", getRes);
            } else {
                gw.connect("/Amfphp/");         
                gw.call("GetSomeData.getData", getRes);
            }
        }

        public function loadArrays():void {
            for(var i:uint = 0; i < someData.length; i++){
                someStrings[i] = new Array();

                //in some cases I had to use .toString().split(",") to get my data
                //into the same state that it was in when I was using 
                //mysql_query with amfphp 1.9
                someStrings[i][0] = someData[i][0].toString().split(",");
                someStrings[i][1] = someData[i][1].toString().split(",");
                someStrings[i][2] = someData[i][2].toString().split(",");
                someStrings[i][3] = someData[i][3].toString().split(",");
                someStrings[i][4] = someData[i][4].toString().split(",");
                someStrings[i][5] = someData[i][5].toString().split(",");
                someStrings[i][6] = someData[i][6].toString().split(",");
                someStrings[i][7] = someData[i][7].toString().split(",");

                if(i == someData.length - 1){
                    arraysLoaded = true;
                }
            }
        }

        private function onResult(responds:Object):void {
            //load column names from php getColumns()
            if(colNames[0] == undefined){
                var i:uint=0;
                for (var j:uint=0; j < responds.length; j++) {
                    for(var y in responds[j]){
                        colNames[i++] = responds[j][y];
                    }
                    if(j == responds.length - 1){
                        refreshData();
                    }
                }
            } else {
                //load someData with arrays
                for (var k:uint=0; k < responds.length; k++) {
                    someData[k] = new Array(); 
                }

                //load data from php getData() and
                //turn amfphp disordered associative array into ordered 
                //indexed array
               for (var m:uint=0; m < responds.length; m++) {
                    for (var n:uint=0; n < colNames.length; n++) {
                        for(var x in responds[m]){

                            //ensure that column name in responds is next in colNames
                            if (x.toString() == colNames[n]){
                                someData[m][n] = new Array(responds[m][x]);
                            }
                        }

                    }

                    if(m == responds.length - 1){
                        loadArrays();
                    }
                }
            }
        }

    }
}