使用来自php的数据在flash中填充数组

时间:2013-03-06 15:54:35

标签: php arrays flash actionscript

嗨所以我需要用flash中的信息填充flash中的数组。我的PHP代码是:

<?php

$db = mysql_connect("localhost","root",""); 
    if (!$db) {
        die("Database connection failed miserably: " . mysql_error());
    }


$db_select = mysql_select_db("profileofperson",$db);
    if (!$db_select) {
        die("Database selection also failed miserably: " . mysql_error());
    }

?>

<html>
    <head>
        <title>mySQLtestfile</title>
    </head>
    <body>
 
<?php
//Step4
$result = mysql_query("SELECT * FROM catalogue", $db);
    if (!$result) {
        die("Database query failed: " . mysql_error());
    }

    while ($row = mysql_fetch_array($result)) {
        echo $row[" Name"]." ".$row["age"]." ".$row["Allergies"]." ".$row["height"]." ".$row["weight"]."<br />";
    }
?>
    </body>
</html>

目前正在显示来自数据库的信息。如何让flash填充到数组中?

1 个答案:

答案 0 :(得分:0)

使您的文档xml而不是html,启动文档如下:

<?php

    header("Content-type: text/xml");
    echo chr(60).chr(63).'xml version="1.0" encoding="utf-8" '.chr(63).chr(62);

这只是添加了一个标头标签,因此浏览器/ flash将文档识别为XML(类似于带有HTML的DOCTSPE):<?xml version="1.0" encoding="UTF-8"?>

然后,按照您的方式进行查询,但以有效的XML格式回显结果:

    echo "<people>";//Create the parent node

    while ($row = mysql_fetch_array($result)) {
            echo "<person>";//Open child node, add values:
            echo "<name>".$row[" Name"]."</name>";
            echo "<age>".$row["age"]."</age>";
            echo "<allergies>".$row["Allergies"]."</allergies>";
            echo "<height>".$row["height"]."</height>";
            echo "<weight>".$row["weight"]."</weight>";
            echo "</person>";//Close child node
    };

    echo "</people>";//Close the parent node

?>

我刚刚把它写在我的头顶,所以可能不完美,但它应该很容易让你检查它是否生成一个有效的XML文档(只需在浏览器中加载页面,得到一个XML查看器插件可以查找更多内容(如果出错)并调整(如果没有),有数百万篇关于从PHP生成XML页面的教程。

然后,使用Flash应用程序中的URLLoader类来访问它:

var loader:URLLoader = new URLLoader();
//The loader class

var urlRQ:URLRequest = new URLRequest('yoursite/yourpage');
//The URL request    

loader.dataFormat = URLLoaderDataFormat.TEXT;
//Use this for xml

urlRQ.method = URLRequestMethod.POST;
//Set method type (normally post)

loader.load(urlRQ);
loader.addEventListener(Event.COMPLETE,loadedData);

然后,让loadedData解析XML:

function loadedData(e:Event):void {

    var people:XML = XML(e.target.data);//Cast this var as XML to access
    //people should represent top-level ie <people>;  

    for each(var person:XML in people.person){//Iterate over member nodes called 'person';
         trace(person.age);//XML object node names can be referenced 
                           //directly with dot notation!
         trace(person.name);//etc...
    }

}

最后一点,在PHP中不推荐使用mysql_函数,我最近发现了这一点,将来会使用SQLi或PDO!

希望这会有所帮助,正如我所说的那样,我已经把它写在了我的头顶,而且你试验它会更好,但如果你遇到困难,请留下评论,我会看看!