Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-18-2004, 09:15 PM   #1
addorian
Registered User
 
Join Date: Dec 2004
Posts: 23
Default parsing XML to PHP

After hours of trying and surfing for answers I finally dare to post my own question.

ok, this is what I want to do:
sending a var in flash to a php script which looks into a MySQL database and parses the data back to flash. I want to use XML to do this.

Flash -> XML -> PHP -> mySQL ->PHP -> XML ->Flash

I've got the last part running, now I only need to send the var from flash to the php and I'm done. I played aroud with the sendAndLoad() function but I didn't get it to work.

Can someone please give me an example of how I should send a var in XML from flash to php and show me how I make the XML into a var again in php so I can use it in a query?

This is my code sofar:
the actionscript:
Code:
function kamerBezoekers(kamer) {
//Dit is de code om de users in een database op te halen
//door een link te openen naar een php script wat XML genereerd
//deze functie maakt een array waarin alle users staan die zich in de kamer bevinden
//zo is de array opgebouwd: bewoner[[id,username],[id,username]]

	//maak de XML die naar de PHP wordt gezonden met het kamernummer
	myKamer = new XML("<kamernr=\""+kamer+"\">");
	myKamer.contentType = "text/xml";
	trace(myKamer.firstChild);
	
	
	//maak de array aan die flash straks gebruikt
	bewoner = new Array();
	// maak het nieuwe XML object aan
	bewonersXML = new XML();
	bewonersXML.ignoreWhite = true;
	// check of de XML binnen is
	bewonersXML.onLoad = function(succes) {
		if (succes) {
			trace("terug");
			maakArray(bewonersXML);
		}
	}
	
	//laat de XML in flash
	bewonersXML.sendAndLoad("http://localhost/testsite/good/xml/kamerbewoners.php",myKamer,"POST");

	function maakArray(xmlDoc_xml) {
	//vorm een array die flash kan gebruiken uit de XML
		root = xmlDoc_xml.firstChild;
		if (root.hasChildNodes()) {
			for (var i = 0; i<xmlDoc_xml.firstChild.childNodes.length; i++) {
				_root.bewoner[i] = new Array();
				_root.bewoner[i].push(xmlDoc_xml.firstChild.childNodes[i].firstChild.firstChild.nodeValue);
				_root.bewoner[i].push(xmlDoc_xml.firstChild.childNodes[i].childNodes[1].firstChild.nodeValue);
			}//for
		}//if
		trace(bewoner);
	}//maakArray()
}//kamerbezoekers();

kamerBezoekers(54);
And the PHP
PHP Code:
<?
//haal inloggegevens op voor database
include('include.php');

//welke kamer?
$kamernr 54;

//$kamernr = file_get_contents("php://input");
//$kamernr = implode(explode("<kamernr=\"", $kamernr));
//$kamernr = implode(explode("\" />",$kamernr));

//maak query & open link naar database
    
$link mysql_connect("localhost"$loginname$loginpass) or die("Could not connect to database: ".mysql_error());
    
mysql_select_db($database) or die("Could not select database ".mysql_error());

//haal de db op, ontrafel de array en print hem als een nieuwe array voor flash        
    
$query ="SELECT * FROM users WHERE room = $kamernr";
    
$result_handle mysql_query($query) or die("query failed ".mysql_error());
        
    
$content "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \n\r";//begin XMLcode
    
$content .= "<bewoners> \n\r"//open bewoners tag
    
    
while($row mysql_fetch_array($result_handleMYSQL_ASSOC)) {
        
//def. vars omdat anders XML ze verneukt
        
$id $row['ID'];
        
$username $row['username'];
        
        
$content .= "<bewoner> \n\r ";
        
$content .= "<ID>$id</ID> \n\r";
        
$content .= "<username>$username</username> \n\r";
        
$content .= "</bewoner> \n\r";
    }
///while
    
    
$content .= "</bewoners> \n\r";
    
    
mysql_close($link);

//poep alle data en vars uit in XML
    
print ($content);
//    print("&resultt=bewonerslijst");
//    print("&loading=NO");
?>
I'm sorry but I've wrote all comments in dutch.
thanks in advance
addorian is offline   Reply With Quote
Old 12-19-2004, 10:00 AM   #2
snapple
member
 
snapple's Avatar
 
Join Date: Feb 2002
Location: London
Posts: 2,442
Default

addorian,

You've made it sound quite complicated, by saying this:

Quote:
Can someone please give me an example of how I should send a var in XML from flash to php and show me how I make the XML into a var again in php so I can use it in a query?
But really (unless i am missunderstanding) all you're trying to do is send a variable from Flash to php? The fact that its in xml is irrelevant (or should be) - i always try to not let my variables 'belong' to any kind of langauge if you see what i mean (i suppose this is a kind of up side to the way flash [used to] communicate with serverside langauges).

So, the data your trying to send is just in the array?

In which case it would be something like this:

ActionScript Code:
_root.myButton_btn.onRelease = function () {     dataSend_lv = new LoadVars();     dataSendBack_lv = new LoadVars();     dataSend_lv.bewonerSend = bewoner;     dataSend_lv.onLoad = function (successGo)     {         if(successGo)         {             trace("worked");         }     }             dataSend_lv.sendAndLoad("myphpFile.php", dataSendBack_lv, "POST"); }

Slightly concerned that you're pushing 2 lots of data into the same subscript of the array, how come?

The php would be:

PHP Code:
<?

$dbHost 
"localhost"
$dbUser "root"
$dbPass "password";
$dbName "databaseName";
$tbName "tableName";

$bewonerSend $_POST['bewonerSend']; 

$res mysql_db_query "database","SELECT attribute FROM tableName WHERE attribute = '$bewonerSend'" );
The above php will not be enought o execute the file, however, i can see from what you have posted that you're capable with php - so you can fill in the blanks.

Regards, snapple

Last edited by snapple; 12-19-2004 at 10:07 AM.
snapple is offline   Reply With Quote
Old 12-19-2004, 01:56 PM   #3
addorian
Registered User
 
Join Date: Dec 2004
Posts: 23
Default

thanks,

but with LoadVars.sendAndLoad() I can't send XML to the flashfile can I?
I need the same php file to receive a variable, and then send XML back to the flashfile so I can use it with a XML.onLoad()

As I understand it now, if I send a variable with LoadVars.sendAndload() flash doesn't understand the code if it is XML. I could be wrong of course but I can't seem to get it to work.

martijn
addorian is offline   Reply With Quote
Old 12-19-2004, 02:06 PM   #4
snapple
member
 
snapple's Avatar
 
Join Date: Feb 2002
Location: London
Posts: 2,442
Default

Now i'm confused. When you say:

Quote:
Flash -> XML -> PHP -> mySQL ->PHP -> XML ->Flash

I've got the last part running, now I only need to send the var from flash to the php and I'm done
But you don't have a flash > php link???

It is irrelevant where the data comes from - just use them as strings and re-serialise them at either end.

If your receiving data from XML into flash, then put it in an array, then send it from flash to php.

I think you're making this more complicated than it needs to be. Just say what you need to do in simple terms.

sentences like "should send a var in XML from flash to php" - don't make sense - you can only send a variable in one form from one place - you mean flash > php - irrespective of where it has come from.

Sorry if i am missunderstanding - but i dont understand what you're trying to do.

Regards, snapple
snapple is offline   Reply With Quote
Old 12-19-2004, 02:19 PM   #5
addorian
Registered User
 
Join Date: Dec 2004
Posts: 23
Default

ok, I want to send a variable from flash to php.
But I want the variable to be in XML so I can load the result from the php script as XML into flash.

The thing is that the php generates an array and I had some trouble to get that array into flash, so I converted the array in php to XML and let flash convert the XML back to an array. If you know of an easier way to get arrays from php into flash I would also be an happy camper

thanks! martijn
addorian is offline   Reply With Quote
Old 12-19-2004, 02:41 PM   #6
snapple
member
 
snapple's Avatar
 
Join Date: Feb 2002
Location: London
Posts: 2,442
Default

I was going to say - there is (in my opinion) no need, apart form making things harder than they need to be, to use XML and PHP together - i have yet to come across a good reason for mixing the two.

And adding an extra markup langauge into the equation certainly won't help.

To send an array to php do this:

(This was a drawing application i built that stored the users x and y coordinates in a MySQL db).

Say i had 2 arrays like this:

array1:

548.95
547.95
545.95
541.95
535.95
528.95
521.95
513.95
505.95
496.95
487.95
477.95
470.95

array2:

548.95
547.95
545.95
541.95
535.95
528.95
521.95
513.95
505.95
496.95
487.95
477.95
470.95

ActionScript Code:
_root.saveArray.onRelease = function () {     imageSend_lv = new LoadVars();     imageBack_lv = new LoadVars();     imageSend.xCoord = mousePointsX;     imageSend.yCoord = mousePointsY;     imageSend.onLoad = function (successGo)     {         if(successGo)         {             trace("data sent");         }     }     imageBack.onLoad = function (successBack)     {         if(successBack)         {             trace("data saved");         }         else        }     imageSend_lv.sendAndLoad("drawing.php", imageBack_lv, "POST"); }
You can receive the array sent to the php file as a normal variable, just like any other php variable being received.

But...say you wanted to get the data back out of the database into php, ready to send back to flash, well, you get it like this (using the same example):

PHP Code:
$tmpStrX "";
$tmpStrY "";

$resData mysql_db_query ("images","SELECT draw_x, draw_y FROM table_name WHERE draw_name = '$varName'");

while (
$entry mysql_fetch_array ($resData))
{
    
$tmpStrX $tmpStrX $entry["draw_x"] . ",";
    
$tmpStrY $tmpStrY $entry["draw_y"] . ",";
}

mysql_free_result($resData);

print 
"myNamesX=$tmpStrX";
print 
"&";
print 
"myNamesY=$tmpStrY";
print 
"&"
Then you'd receive that in flash like:

ActionScript Code:
image_lv = new LoadVars(); image_lvBack = new LoadVars(); image_lv.varName = getUser(this._name, storeNames); image_lv.onLoad = function (checkPlease) {     if(checkPlease)     {         _root.banner.myStatus.text = "Work of art retrieved";     } } image_lvBack.onLoad = function () {         unescape(this);     _root.banner.myStatus.text = "Work of art retrieved"; } image_lv.sendAndLoad("genDraw_x.php", image_lvBack, "POST"); storeX = image_lvBack.myNamesX.split(","); storeY = image_lvBack.myNamesY.split(",");

and hey presto!!! You have an array called storeX and storeY - with the data serialised! and array from flash > php > mysql > php > flash.

No need for nasty XML.

Regards, snapple
snapple is offline   Reply With Quote
Old 12-19-2004, 06:06 PM   #7
addorian
Registered User
 
Join Date: Dec 2004
Posts: 23
Default

Ok, thanks for your help!
I've now got a simple thingy which sends an variable to php, the php returns teh variable back to flash (usefull isn't it )

This is the code:
AS
Code:
function kamerBezoekers(kamer) {
	var mySend_lv = new LoadVars();
	mySend_lv.kamer = 54;
	var myReceive_lv = new LoadVars();
	myReceive_lv.onLoad = function(succes:Boolean){
		if(succes){
			trace("succeed");
			trace("---");
			trace(myReceive_lv.verstuur);
			trace("---");
		}else{
			trace("fail");
		}//if/else
	}//onLoad = function
	trace(mySend_lv.sendAndLoad("http://localhost/testsite/good/sendAndLoadVar.php",myReceive_lv,"POST"));
	
}//kamerBezoekers()

kamerBezoekers(54);
and the PHP
HTML Code:
<?php
$ontvangen = $_POST['kamer']; 

print "verstuur=$ontvangen"; 
?>
addorian is offline   Reply With Quote
Old 07-20-2006, 03:01 PM   #8
kemso
Registered User
 
Join Date: Jul 2006
Posts: 1
Send a message via AIM to kemso
Default Receive XML response with LoadVars

It seems that what you were originally trying to do was post some variables to a PHP page, process them with the PHP (Store to mysql, etc.) then return an XML doc to flash.

I have just been trying to do the same thing. All you need to do is set up a LoadVars object and make the target an XML object rather than a LoadVars object. like so:

Code:
var send_lv:LoadVars = new LoadVars();
var responseXML = new XML();
responseXML.ignoreWhite = true;
responseXML.onLoad = function(success){
     // handle XML doc
};
send_lv.name = "kemso";
send_lv.sendAndLoad("http://www.someurl.com", responseXML, "POST");
Hope that helps.
kemso is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:11 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.