Join 136,420 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,361 people online right now. Registration is fast and FREE... Join Now!
In this tutorial we’ll see how to pass complex data from php to flash, without using AmfPhp but also without the “classic” format readable by flash…this can bea n intermediate solution: AmfPhp is for large amount of data, and the classic “var=value&var2=value2&…” is for small/simple data-types…what can we do when we have a little amount of complex data, or a medium amount of simple/complex data ? Well…we can use the serializer class!
1b) What we need
a) Serializer Class (downloadable at [URL=http://sourceforge.net/projects/serializerclass/ )]http://sourceforge.net/projects/serializerclass/)[/URL] – An excellent class, a Sephiroth’s ( http://www.sephiroth.it ) idea that became an useful realty As said before, this class allows to pass complex dates from php to flash without the “classic” format
Var=value&var2=value2….
that with a medium amont of data can be very very boring and hard to use, but it allows to pass directly more complex objects (eg Arrays) This is the official class description
“Serializer Class enable (For Macromedia Flash) you to pass and receive complex data type from and to PHP and Flash using the native PHP functions serialize and unserialize. Flash received data will be a native Flash object”
Flash mx 2004 (I think you know it , if you haven’t it you can find a trial on www.macromedia.com)
c) A local webserver, or an hosting, where test our little example (it must have PHP support)
d) Macromedia Extension Manager (free, downloadable from www.macromedia.com), wich we’ll use for install the Serializer Class Extension
1c) Last notes before start
In this tutorial, we see simply how the data-passage works, we won’t use database, .txt files and other, but only a .php files with “fixed” data….obviously you can take data wherever you want, but I thought that use a data-source (eg a database) in this tutorial hadn’t a practice utility, it’d only make the tutorial more complex and longer…I preferred made things simple …it will your work make it harder
2) Let’s go!
Well, before start, I created the files (serializer.fla and serializer .php). I’ve also downloaded the .mxp file.
NOTE: Obviously you can also create .fla and .php during the tutorial, and not immediately…I preferred create now the files for show how they’ll be
2a) Serializer Class installation
Now, first thing to do is install the Serializer class. Double click on .mxp file … follow the video instructions you find at the URL .
As you see, after the installation, you’ll have a “Serializer” name into the Extension Manager (ehm..I’ve a lot of extension installed now, but if you have less caos then me you found it immediately ). Selecting the Serializer into the Extension Manager you can see a little sample-ActionScript code. You can see with your eyes, it’s very easy. Now I’ll explain you pass-by-pass how to try if the class works correctly, with a small example.
2b) The PHP file
For class testing, we need a php file that have to pass to flash some complex data (array, objects, etc). Let’s write, into “Serializer.php”, some simple code-lines with an array containing different object types…
Now, we can see if it really pass data urlencoded and serialized…then we can put the .php on our webspace (or into our local test directory) and visit it in a brower.
After the PHP code, it’s time to open Flash Create Serializer.fla (or open it), and in the first frame write this code
CODE
import it.sephiroth.Serializer
var serial:Serializer = new Serializer(); // import from PHP lV = new LoadVars(); lV.onLoad = function(){ unserialized = serial.unserialize(this.ToFlash); } lV.load('http://www.brolyweb.com/Tutorials/Serializer/Serializer.php');
(change the path into lV.load to your. Php file)
See the video at URL.
As you see, after inserted the code, if you test your flash movie, you’ll have a variable that have the same values that where in the PHP array…simple, true?
3) Bye…and a preview
In this tutorial we saw how to receive serialized data from php…but if we’d do the vice-versa, send serialized data from flash to php? Well, it’s that we’ll see in next tutorial, friends I hope you like this tutorials with video inserts that show some passages , tell me if you like them!!! (please note: I haven't insert the .swf videos into the thread because they were too large, if I publish it at less than that size they became incomprensible )
This post has been edited by Broly: 17 Dec, 2004 - 04:15 PM
uhh now that i have som numbers in some serialize thing, ofcourse risking sounding very stupid, how do i use them, say put them in an array in flash =) I have an array called nums how do i get the numbers into it ?
//cheers
p.s. while im at it, how do i see the amount of values in an array, is it something like array'last ? (thats the ADA version) :S
var serial:Serializer = new Serializer(); // import from PHP lV = new LoadVars(); lV.onLoad = function(){ unserialized = serial.unserialize(this.ToFlash); }
lV.load('http://localhost/test3.php');
/////////////////// this.pathToPics = "images/pics/"; // fill array with pics this.pArray = unserialized; // if you just declare this here as an array it // it works fine e.g. ["image0.jpg", "image1.jpg", "image2.jpg", "image3.jpg"];
Could anyone help pls
This post has been edited by nexx: 1 Dec, 2005 - 01:36 PM