I am building an air app in flash cs6 using as3. I need to send an array from php to flash as3.0 I wanted to create a "timeline" on my application. I have a lot of reading various references, but not much help. This is the code I use. timeline.php file
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]";
}
}
}
and here is as3.0 code
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]);
}
}
but I have error.
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()
please help me
Do not assemble your data into a string as you do it. In your case, the result is not a valid url encoded string (missing the &, for example) and it doesn't automatically convert into an array either as you expect it in your code.
JSON or XML is a better format to exchange data, especially when it has some kind of structure. I use the former for this answer.
Speaking of structure, distributing the data that belongs together over several arrays is a bad structure. Don't have many arrays with single properties, but one array instead, with objects in it that groups the properties together.
In your case, one object has the properties name
, posting
and ID
, which would look something like this in JSON:
{
"name":"foo",
"posting":"bar",
"ID":"123"
}
As you have many such objects, they are in an array:
[
{
"name":"foo",
"posting":"bar",
"ID":"123"
},
{
"name":"foo000oo",
"posting":"baAAaar",
"ID":"456"
}
]
This is what the body of the response from your server could look like.
In PHP, you have mysqli_fetch_assoc()
and json_encode()
to get your data from the database into a JSON representation as you can see in this other answer
On the client side (and yes, @Jonny Henly is right hat you should not nest functions into each other!) you JSON.parse() the received string and get a generic As3 Object
as a result:
function onCompleteLoadTimeline(event:Event)
{
var result:Object = JSON.parse(event.target.data);
trace (result[0].name); // should give first name
}