220 likes | 423 Views
ActionScript & PHP. Loading External Data in Flash. Loading external data using ActionScript 3.0 Advantages: Good for loading data that will be changed periodically.
E N D
Loading External Data in Flash • Loading external data using ActionScript 3.0 • Advantages: • Good for loading data that will be changed periodically. • No need to open Flash, edit a file, re-publish, and upload the new file, you can just pull all the data from an external source file and display within the Flash app. (Can even render the text as HTML).
Loading External Data in Flash • The flash.net package contains classes for sending and receiving from the network, such as URL downloading and Flash Remoting. • Use the URLRequest,URLLoader and URLVariablesclasses. You use: • URLRequest to link your Flash document to an external file, or website. • URLLoader to load the data into your Flash movie. • URLVariables to transfer variables between a Flash application and a server.
Set up Flash objects varvariables:URLVariables = new URLVariables(); varrequest:URLRequest = new URLRequest(); varloader:URLLoader = new URLLoader(); request.url = "change.php"; Equivalent to setting action in HTML
Loading External Text in Flash (AS3) • Example: load an external file into a Flash application and display it in a text box. • Use a TextArea component. Drag a TextArea component to the stage, and give it an instance name, i.e. external_faq. • AS3 code goes in the timeline on the same frame as the objects it references.
Loading External Text in Flash (AS3) // Step 1 varfaqLoader:URLLoader = new URLLoader(); // Step 2 faqLoader.load(new URLRequest("test.txt")); // Step 3 faqLoader.addEventListener(Event.COMPLETE, onComplete); // Step 4 function onComplete (event:Event):void { // Step 5 varfaq:String = event.target.data; // Step 6 external_faq.text += faq; } Step 1 Create a new URLLoader: define the variable faqLoader as a URLLoader, then call a new URLLoader() object. The URLLoader will hold the data returned by the URL. Step 2 Load the url, using the URLRequest() object. Use the load method of the URLLoader object, and open a new URLRequest object, which contains the path to a text file. Step 3 Add an Event Listener to the faqLoader to listen for when the loading has been completed. Step 4 Set a String variable to hold the text that is loaded from the file. This will enable you to do any string manipulation you may wish to do. In this case just display the text in the TextArea component, when the onComplete function executes. Step 5 Set the text property of the TextArea component to the String variable. Note the use of external_faq.text += faq, instead of external_faq.text = faq. (To use html formatted text, you would use the htmlText property.) Step 6 The text file should contain the text you wish to display.
GET vs POST: Two ways to transfer data between Flash and server-side scripts: GET or POST • Using GET - concatenates variable name/values into the URL • http://www.wdim.aiwsites.com/info.php?firstname=Johny&lastname=Smith • This method is not very secure and also has a 1024-character limitation • Using POST, the data is contained in the header of the HTTP request • POST does not have a character limit
How does Flash get external values? Variable-Value Format Flash needs to receive the variables and values in the format below: For a single variable: variable1=value1 Similarly for multiple variables it needs the same format, but separated by "&" in between them: &variable1=value1&variable2=value2&variable3=value3&variable4=value The server side page interacts with the Database and gets the required data and sends the retrieved data to Flash. Flash needs to receive the variables and values in a similar format from the PHP file as shown below: echo "&name=$fldName&email=$fldEmail&mess=$fldMess&date=$fldDate • Note: The variable name should be understandable by you as to what it represents and shouldn't include spaces. Spaces should not be present between the '&', the variable name and the '=' sign. The values can have spaces.
Variables out of PHP • Flash can receive data in name-value pairs (variables in the format of name=value). If there is more than one pair then each name-value pair is separated by an ampersand, and so a typical string of name-value pairs may look like this echoed or printed: &name1=Neil&location1=Bristol&name2=Justin&location2=London& echo “&name1=Neil&location1=Bristol&name2=Justin&location2=London&”; • The final ampersand is optional.
Loading Variables into Flash • The values of the variables can be accessed from Flash with the help of the variable names specified in the external text file into any Dynamic text field, Textarea etc. in Flash.
Sending back to Flash • If Flash is expecting a value to be returned then it will pick up the returned value(s) (from a server side script) and you can use it in your Flash movie. • You can use the PHP language construct echo to output the value(s) from PHP • Return the value(s) in "name-value pair" format expected by Flash.
Sending back to Flash • PHP can also output data in XML format • Echo it in XML format so that Flash can use its XML methods to parse it. • Save the data to an XML file so that Flash can use its XML methods to parse it.
MySQL Interaction Utilizing MySQL and PHP allows Flash to display and retrieve dynamic data, among other things
Flash – PHP – MySQL Interaction A file is accessed by a browser Webpage/SWF When PHP processing/output is complete, Display data in flash Flash Interaction PHP PHP MySQL
MySQL - Flash • MySQL communicates with PHP (or other language) that can format data into a format that Flash understands. • You do not change how PHP connects to or queries the MySQL database, instead you change how PHP outputs the returned data. • Flash variables format or XML format • You treat the variables received from Flash the same as you would treat GET or POSt variables received from an HTML page or form. • Make
PHP Output • XML output from MySQL example • http://onaje.com/content/transforming-dynamically-generated-xml-html • Flash Vars format … connect to DB, select rows print <?xml version="1.0"?>';print '<result>';while($row = mysql_fetch_array($query)){print "<row>";… decide how to format/output retrieved rowsprint "</row>";}print "</result>";
Example: Poll • Flash MySQL Poll • Have visitors answer a simple poll of a few questions about your project. • On frame/In Movie that displays media, have a button that records the visitor poll vote and send the info to a PHP script that saves it into the MySQL database • Display poll results before and after visitors answer the question(s) i.e. if no one has answered the poll yet, flash should say so, then once at least one person has answered Flash updates the message to say there has been one participant etc. and show the current poll results.
Example: Rate/Vote • Flash MySql Rating/Voting • Have visitors rate images, songs or videos used in project • On frame/In Movie that displays media, have a button that records the visitor vote and send the info to a PHP script that saves it into the MySQL database • Display rating results before and after visitor vote i.e. if no one has rated the image, it (Flash) should say so then once at least one person has voted, update the message to say there has been one vote, etc. and show the current vote results.
Example: Guestbook/Comments • Flash MySQL Guestbook/Comments • Have visitors enter comments about project into input fields in Flash, send info to PHP • PHP script saves comments into the MySQL database, returns success or failure to save comment to Flash • Display comments before and after visitor comment i.e. if no one has commented, it (Flash) should say so then once at least one person has commented, update the message to say there has been one comment or just show the comment in a list of comments immediately, etc.
Example: Login • Flash MySQL Login • User login system: have visitors login info into input fields in Flash, send info to PHP • PHP script checks login info against what is saved in the MySQL database, returns success or failure to login to Flash
Example: Use Imagination • Use your imagination to think of other ways to interact • Possible to satisfy two requirements of the project in one: • If you had an image gallery or mp3 player as part of your project, instead of storing media data in a text/XML file, store the data in MySQL • Use PHP to create text/XML file with the location of the media, title, caption, author etc. -> Flash uses this file to load media data. • Flash displays media with rating buttons/input fields, send rating data back to MySQL via PHP • *Could also create a backend in Flash that allows the Flash site administrator to edit/update the media info – file location, title, author etc. – stored in the MySQL database from Flash (via PHP) • PHP updates the text/XML file when database is changed.
Tip for Success • At this point in the course you are familiar with importing external text and XML • If using PHP to output text/XML, you process that output the same way you would an XML/text file not produced by PHP • (you would probably tell Flash to open the text/XML file created by PHP, not the PHP script) • If using PHP to pass variables i.e. in Flash vars format, then Flash would request the PHP file directly • *** Whatever the Scenario: Make sure your PHP code works.***