PDA

View Full Version : Actionscript + PHP


ebrett
02-27-2009, 03:25 PM
I need some help. Basically i'm trying to connect my Sandy3d project to a MySQL database. I understand actionscript cannot directly link to the database, so you use PHP to create a string which actionscript understands.

This part i'm alright with, but where i'm stuck is understanding how to perform a loop function to extract this data into my project within actionscript.

This is my code atm, which imports a model into Sandy. What I would like to do is modify the code so it pulls 'filename' from the PHP string then moves onto the next record with a loop.

I just can't get my head around how to go about doing this. Please help, thanks.

public function Example()
{
var parser:IParser = Parser.create("assets/main1.ASE",Parser.ASE );
var parser1:IParser = Parser.create("assets/main2.ASE",Parser.ASE );
var parser2:IParser = Parser.create("assets/models/ASE/media.ASE",Parser.ASE );

parserStack = new ParserStack();
parserStack.add("main1",parser);
parserStack.add("main2",parser1);
parserStack.add("media",parser2);
parserStack.addEventListener(ParserStack.COMPLETE, parserComplete );
parserStack.start();

}

ebrett
02-28-2009, 02:09 AM
This is drivig me crazy, but I think i'm getting somewhere. I need someone who knows more about actionscript 3.0 though. I had some code, which starting pulling data from the PHP string however i've now found AS 3.0 wont support loadVars, so it needs modifying and i'm abit stuck.

Actionscript 2.0 code (working)


var sender:LoadVars = new LoadVars();
var receiver:LoadVars = new LoadVars();
receiver.onLoad = function(ok){
if(ok){
for(var i =1;i<=receiver.total;i++){
receiver["dataPacket"+i] = receiver["user_data"+(i)].split("|");
var _id:String = receiver["dataPacket"+i][0];
var _xCoord:String = receiver["dataPacket"+i][1];
var _yCoord:String = receiver["dataPacket"+i][2];
var _zCoord:String = receiver["dataPacket"+i][3];
var _nameLong:String = receiver["dataPacket"+i][4];
var _description:String = receiver["dataPacket"+i][5];
delete(receiver["user_data"+i]);

trace( "id=" + _id + " xCoord=" + _xCoord + " yCoord=" + _yCoord + " zCoord=" + _zCoord + " Description=" + _description );
trace("");
}
}else{
trace("error loading PHP file");
}
}

sender.sendAndLoad("http://edd.servehttp.com/fyp/getBuildingDetails.php",receiver,"post");

What I need is the version below to work. I have started editing to AS 3.0 but theres an error with lines 13 and 17.


import flash.net.URLLoader
import flash.net.URLRequest

var receiver:URLLoader;
receiver = new URLLoader();


var req:URLRequest = new URLRequest("http://edd.servehttp.com/fyp/getBuildingDetails.php");

receiver.onLoad = function(ok){
if(ok){
for(var i =1;i<=receiver.total;i++){
receiver["dataPacket"+i] = receiver["user_data"+(i)].split("|");
var _id:String = receiver["dataPacket"+i][0];
var _xCoord:String = receiver["dataPacket"+i][1];
var _yCoord:String = receiver["dataPacket"+i][2];
var _zCoord:String = receiver["dataPacket"+i][3];
var _nameLong:String = receiver["dataPacket"+i][4];
var _description:String = receiver["dataPacket"+i][0];

delete(receiver["user_data"+i]);
trace( "id=" + _id + " xCoord=" + _xCoord + " yCoord=" + _yCoord + " zCoord=" + _zCoord + " Description=" + _description );
trace("");
}
}else{
trace("error loading PHP file");
}
}

RelaxGuy
02-28-2009, 02:50 AM
So you want to send a string from PHP to Flash correct?
I've never used AS2 or Sandy 3D but this should help with your AS3:

import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;

//new URLLoader, set the data type
var receiver:URLLoader = new URLLoader();
receiver.dataFormat = URLLoaderDataFormat.TEXT;
//.php file we want to call
var req:URLRequest = new URLRequest("http://edd.servehttp.com/fyp/getBuildingDetails.php");
//listen for when the urlLoad is complete
receiver.addEventListener(Event.COMPLETE, loadingComplete);
//initialize urlLoad
receiver.load(req);

function loadingComplete(event:Event):void {
//get a reference to the URLLoader;
var receiver:URLLoader = URLLoader(event.target);
// look at the incoming data, your PHP string should show up here
trace(receiver.data);
}

ebrett
02-28-2009, 03:35 AM
I actually found an example very similar to yours on the internet, which I got working. Yours however, seems more logical, so i probs go with that.

Problem i'm having now of course is spliting up the data so its useful. Need a For Loop like the AS 2.0 example so i can extract the data to variables. Everything I try however wont work. I understand what the AS 2.0 example is doing, however I lack enough knowledge to make it work with AS 3.0. :eek:

ebrett
02-28-2009, 10:52 AM
All is well now. Decided to create an XML page using PHP as its alot easier to reference in Actionscript. :)