我最近升级到了php 5.6和amfphp 2.2.2,并且尝试修改几年前编写的Actionscipt 3动画中处理数据的方式(我已经很长时间没有使用AS3了,成为问题的一部分)。
amfphp网站(Silex Labs)说,仅对数据使用PDO,而AS3和amfphp可以正确处理它,但这是行不通的。
AS3“响应者”类处理来自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;
}
?>
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
}
}
最佳答案
我能够使我所有的AS3动画正常工作,并且我想分享我对将PHP 5.6.2与Amfphp 2.2.2和AS3一起使用的发现。我认为我的AS3代码看起来有些过头,但是经过3天的努力,这是我能想到的最好的方法,并且可以正常工作。
我的主要目的是使数据数组的结构恢复到使用php 5.2,amfphp 1.9和mysql_query()时的状态。
Amfphp调用GetSomeData.php中的getData()函数,该函数显然使用其SELECT语句。但是Amfphp会以无序状态而不是SELECT语句中指定的顺序将数据返回给Actionscript。此无序状态不是按字母顺序排列的列名,也不是按最小字段大小顺序排列的。它似乎是随机的。
但是,Amfphp确实以SELECT顺序返回在getColumns()中选择的列名。因此,我使用了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;
}
}
?>
如果从此脚本中删除“类GetSomeData”和“公共静态”,并在getColumns()中使用print_r($ results)并从网页运行它,您将以SELECT顺序查看各列:
数组([0] =>数组([col1] => 1.01 [col2] => 1.23 [col3] => 2.23 [col4] => 1.31 [col5] => 2.44 [col6] => 1.33 [col7] => 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();
}
}
}
}
}
}
关于php - 子数组在Amfphp,Actionscript 3数据集成中消失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43380835/