|
||||||||||||||
. . . . Elena Tresh Foundation Florida . |
Flash MX Database - Read & Write to a text file in FlashExploring loadVars.sendAndLoad, write to a text file and get the data back into flashby Deamothul
hello people lets take a look at loadVars.sendAndLoad and what we could do with it today.
Flash Database Pre setup:Go to your web folder and make a folder named "exercise". If you havent setup a localhost yet you could tryout my getting php data to flash tutorial.In that tutorial i explained how to setup testing environment for php via a little gem named "phpdev" ( sets up apache server, php, phpmyadmin... i love it). So if you followed that tut, your webroot is c:\phpdev\www\. so you make the "exercise" folder there and make sure you're apache server is running. So now you should have something like: c:\phpdev\www\exercise\ You can browse to that folder by opening up your favorite browser and type as url: http://localhost/ and your apache environment should show up. now just browse to the folder you made and you are set. - Now Openup notepad or some text editor and make a new file. - Save this empty text file as "myTextFile.txt" in the exercise folder( c:\phpdev\www\exercise)
Step 1: Setting up the php part- Openup your favorite editor (i used sciTE|FLASH)- Make a new file and save it as careTaker.php in the exercise folder. - Now place this code in the php file.
[PHP open TAGS HERE]
//phps opening tags are "<" + "?"
//these are the variables that will
//hold data sent from flash via input textbox
//data is sent by sending part of loadVars object
//inputData is a variable we will set in flash,
// its a variable attached
//to the loadVars object and will be send to php
$receivedFromFlashData = $_POST['inputData'];
//this is to make our textfile readable and writable
//place it in a variable so we can refer back to it easily.
// ( @ symbol is to hold back certain error messages)
$myTextFileHandler = @fopen("myTextFile.txt","r+");
// With @file we will turn our textfile
// into an array so we can know how
// many entries there are in the file.
$txtfileArray = @file("myTextFile.txt");
//here we check if we could open the file
// if everything is ok , go on.
if($myTextFileHandler){
//a print to help us
// so this will not be send to flash.
print("txtFile is opened\n");
//loop through the text file array
//remember -> @file("membersDatabase.txt");
//use $count variable in the string we send to text file,
// it attaches a number to the string for us
//to show how may times we inserted something into textfile.
foreach($txtfileArray as $count => $member);
//We go to the last byte of the file,
// because we want to insert data at end of the file
$gotoLastByteOfTxTFile
= @fseek($myTextFileHandler,0,SEEK_END);
//Increase count by one
//because an array starts counting at 0.
$count = $count + 1;
// make a variable writeInTxtFile with command @fwrite.
//It will write a string into the text file
//defined in myTextFileHandler,
//notice the \n makes it start on a new line each time .
$writeInTxtFile = @fwrite($myTextFileHandler,
"\nreceived from flash $count=$receivedFromFlashData");
//Was writing into the txtfile a success ,
// if true print a variable to flash to be handled
// in the response loadVars Object.
if($writeInTxtFile){
// make a variable to hold the message in flash
// if writing to the textfile was a succes.
$writeStatus = "writing to textfile was a succes";
//printed to flash and this data is actually attached to
// the loadvars object we defined in flash.
// can acces like a variable in a movieclip or object.
//can set this variable to be show in a textbox like this:
// mytextbox.text = myReveivingLoadvars.writeStatus
// this piece of code must be triggered in the
// myReveivingLoadvars.onLoad part
print("&writeStatus=$writeStatus");
//if writing was a failure we print that to flash
}else{
$writeStatus = "writing to textfile was a big failure";
print("&writeStatus=$writeStatus");
};
//here we close the stream to the textfile
@fclose($myTextFileHandler);
// If opening the text file was a failure in
//the first place we print this to the php output.
// You can also send error messages like this to flash,
// just change the print command to something like:
//print("&failMsg = $youErrorMsg");
//and call the failMsg variable in your flash
// file in the receiving onLoad part of
//the loadVars object e.g. like
// myTextBox.text = myReceiveLV.failMsg.
}else{
print("opening txtfile has failed\n");
};
//this is printed to flash and received by the reveiver
// part of our loadVars object.
// This piece of data will be show in a textbox
// in flash immediately after a you press submit.
// so you sumbit your inputted text in flash ,
//its ran through this little
// engine like php script and you immediately receive
// an answer back.
print("&receivedData=$receivedFromFlashData");
[PHP closingTAGS HERE]//"?" + ">"
Step 2: Setting up the flash file- make a new flash file: 300 x 300 with a dark grey background- Make an input textbox named "inputData" - Place a static textfield next to it with this text (or something like it) "input some text to store:" - Draw a little square and convert it into a movie clip (select it and press f8) this will become the submit button. - In the convert to symbol menu set these values: * name to "submit" * behavior to movieclip * registration point in the middle * export for actionscript must be selected. - Select the clip again and set its instance name also to "submit" - Make a dynamic textbox to display the status of writing to the textfile. - Give that dynamic textbox an instance name: "statustxtb" and set its dimensions to about 270 x 17. - Make another dynamic textbox, instance name "responsetxtb" and about the same dimension as the input textbox. - This last textbox will hold the response we get from the php file and displays the data we inputted in the input field. - You could choose to set some describing static textfields next to the dynamic text field, but that a personal choice Step 3: Setting up the actionscript part
- Make a new layer in your fla and name it code place this code on the timeline. Well that was my tutorial about loadVars.sendAndLoad in combination with some txt file manipulation, i hope you enjoyed it.Download the files here Untill nextime, Deamothul - The Scriptkeeper - You can contact me at: i_love_actionscript at hotmail dot com |
|
||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||