|
Deamothuls'
getting some PHP data into Flash MX tutorial
Allrighty then, lets
get some external data into flash using php. We're not going
to use a MySql databse this time, that's something for the
next tutorial. In this example we're going to fill a
comboBox, a txtbox, and a listBox in flash
with data taken from a php file. To complete this tutorial
you''ll have to have access to a webserver and have php
enabled on your sytem.
If you don't know what im talking about, download this little
app: phpdev.
phpdev will setup: an apacheServer ( the webserver),
php, MySql and phpMyAdmin, and
you don't have to do nothing else than download the executable,
run it, and everything will be done automatically for ya
without any troubles (i hope).
this is the link to their site:
http://www.firepages.com.au/
And here is a link to the file you will have to install
to follow along with this tutorial, its a true gem this
little app :)
http://prdownloads.sourceforge.net/phpdev5/phpdev423.exe
After the installation a folder will be made on your system
named phpdev (default). In this folder there will be a folder
named "www", this will be your web root and will be the
place where you will test your php files. At the end of
the installation, if everything went ok, the server will
be executed and you will be taken to the web _root in your
favourite browser. If not, fire up your favourite browser
and type http://localhost/ in your url box. You will
see an environment you probably have seen before surfing
on the net.
Figure 1:
This is how the apacheServer environment looks like.
|
|
So to give a quick example, when I would
write I little php file that should print out some string,
running that file normally wont do anything but open the
file and show the source in a text editor. It wouldn't execute
the php file and do the stuff you want like print some value.
The magic starts after you copy that phpfile into the www
foder (remember c:\phpdev\www). Make sure
you have the apache server running ( just press the link
that is made in your programs folder). After you did that
you have to fire up your favourite browser, type http://localhost/
and you are taken to your web _root(just like in the pic
above). If everything went ok, you will see the php file
you just copied into the www folder. Now when I would press
the file to execute it (the php file), the php file is handled
correctly and spits out a string (in this case).
Ok php is now setup (not even talking about mysql and phpmyadmin),
we are now ready to do some php to flash stuff and even
better , your system is now prepared for further experiences
with php, the database MySql, and the MySql database manager
phpMyAdmin. Time for some code. Let us first write the php
file that holds the data that we'll send to flash.
In this example we will place 9 variables in a php script
and send those variables to flash. The first three will
be passed to a combobox, the next three go to three txtboxes
and the last three pieces of data will loaded into a listbox.
So now fire up your favourite editor ( I use sciTE|Flash
) and write this piece of phpcode in it. Then save it as
myData.php. (Or just download It)
b.t.w. Dont forget to place this php code between the php
tags:
"<" + "?" at the start
The php code comes here.
"?" + ">" at the end
both without the " "
This is the php Code.
$dataForCombobox_1 = "This is our FIRST variable,
get ready combobox here I come";
$dataForCombobox_2 = "This is our SECOND variable, get ready
combobox here I come";
$dataForCombobox_3 = "This is our THIRD variable, get ready
combobox here I come";
$dataForListbox_1 = "This is our FIRST variable, get ready
listbox here I come";
$dataForListbox_2 = "This is our SECOND variable, get ready
listbox here I come";
$dataForListbox_3 = "This is our THIRD variable, get ready
listbox here I come";
$dataForTxtbox_1 = "This is our FIRST variable, get ready
textbox here I come";
$dataForTxtbox_2 = "This is our SECOND variable, get ready
textbox here I come";
$dataForTxtbox_3 = "This is our THIRD variable, get ready
textbox here I come";
print("&comboData1=$dataForCombobox_1");
print("&comboData2=$dataForCombobox_2");
print("&comboData3=$dataForCombobox_3");
print("&listbData1=$dataForListbox_1");
print("&listbData2=$dataForListbox_2");
print("&listbData3=$dataForListbox_3");
print("&textbData1=$dataForTxtbox_1");
print("&textbData2=$dataForTxtbox_2");
print("&textbData3=$dataForTxtbox_3");
The file should explain
itself pretty well I guess. First we set 9 variables to
hold a string of text, this is the text we want to show
in flash. I gave the variables names that shows something
about it, so that should make it a bit easier to understand.
In php a variable needs to be declared with a $ sign, and
php isnt as forgiving about the ; sign. you'll have to place
em. So then we assign that php variable(with the $) to a
variable that we'll send to flash via the print command.
The variables that are being send to flash all have the
& sign, just as when we send a variable via a text file
to flash.
If I would run this file through my browser (after copying
it into my www folder), it would give me the output as show
in figure 2. Just look at this output carefully and look
at the php script carefully and you will see the logic behind
it. Because we get the output we want, we know the php script
is functioning correctly. Before you start coding the flash
part, always make sure you get the right output from the
php file. That saves you a lotta trouble and screaming at
the computer ;)
If all went ok you now know How to test the php file,
remember:
1: Copy your php file to the www folder.
2: Make sure apache server is running.
3: Fire up your favorite browser and type in the url box:
http://localhost/
4: Browse to your php file and press it to execute it.
If the file is working correctly the output is shown
e.g. as in figure 2 and you can start to think about opening
flash.
Figure 2:
the output of the php file.
Well peoples, finally, lets open up flash
mx. We only need a little bit of code in the main timeline
of our swf. We're going to use the loadVars object to receive
our data from the php file. So flash will receive the variables
we printed in the php file and those variables contain the
text strings that we wanna display.
This is the flash code that needs to be placed in the mainTimeline
on a frame.
//First we make
a new loadvars object to hold the variables that are being
loaded from the php file.
myData = new LoadVars();
//this is the part where we execute
the function that handles the loaded data.
myData.onLoad = function(){
placeTheDataIntoTheRightPlace();//call the function that
handles the data.
};
//here we load in the php file,
make sure you set the right path to your file!
myData.load("http://localhost/myData.php");
//This is the function that handles
the actual data.
//The variables now
sorta live in the loadVars object we set named myData so
we can call em like:
//myData.myVariableInthePhpPrintedString.
//Just look at the
script carefully and check how the variables are being called.
//Then it should become
all a bit more clear to ya.
placeTheDataIntoTheRightPlace = function(){
myComboBox.addItemAt(0,myData.comboData1);
myComboBox.addItemAt(1,myData.comboData2);
myComboBox.addItemAt(2,myData.comboData3);
myListBox.addItemAt(0,myData.listbData1);
myListBox.addItemAt(1,myData.listbData2);
myListBox.addItemAt(2,myData.listbData3);
txtBox1.text = myData.textbData1;
txtBox2.text = myData.textbData2;
txtBox3.text = myData.textbData3;
};
If everything went ok, you now have a functioning
testing environment for php. Having setup a localhost can
really open doors for you. Now that u know what it is and
how you can send some data to flash possibilities are growing
. I hope everything went ok 4 you, cause i know how frustrating
things get when its going all wrong :D
Greetz from Deamothul -the
Script Keeper-
Any questions you have about this tutorial i will be glad
to answer, (but keep in mind, im just a normal guy struggling
in this world of code and other stuff, im really no pro,
(hehe, although i try) i just love to script).
You can contact me at: i_love_actionscript@hotmail.com
Figure 3: This is how the movie
could finally look, check the file thats up for download.
|