| Ultrashock Tutorials > Flash4 > Building a Flash4 Guestbook | ||||
|
||||
| Building a Flash4 Guestbook | ||||
Data In, Data OutThe combination of Flash and server programming can be used to produce some killer sites. Flash can pass values back and forth between programs running on a server and the .swf file playing on the client computer. The conversation is founded on the rules of the Common Gateway Interface (CGI) - the same that govern the use of html forms. Server scripts can be written in a variety of languages such Perl, C++, Python, ASP and PHP. I'm not going to attempt to cover these, but instead focus on the flash end of things. As a flash developer you will need to understand the flash spin on these things so that you can sit down with your friendly neighbourhood server geek and sort out how your programs are going to talk to each other. A guestbook is a good example of how this conversation between flash and server takes place. In this case, the scripting is done in Perl (you are free to download and install the script). Loading the Entries The very first frame of the flash file contains the actions to load the latest entries. The server script is designed to send back a list of values representing the entries. It takes two parameters: list is the number of items we are asking for and start is the id# of the first item. The start variable is optional and, if left out, the script assumes you want the most recent entries. You have to put checks in place to confirm that the script has sucessfully returned data. As we'll see in a moment, calls to the script always return a value called i_items. Before each call I set this value to "null" so that I can confirm when results have been sucessfully loaded. The timeOut and loadTime variables are used in the loading sequence. The script measures the time after the initial call and if the values have not loaded in 10 seconds then it displays a timeout error message. Finally, we get to the Load Variables action. This action can be used to load name value pairs from a text file. But in this case we're calling on a Perl script to return something that looks as if it's a text file.
Set Variable: "timeOut" = "10000"
Set Variable: "list" = "12"
Set Variable: "i_items" = "null"
Set Variable: "loadTime" = GetTimer
Load Variables ("/cgi-bin/guestbook.cgi?list="&list, 0)
Go to and Play ("loading")
Waiting for Results The loading frame is a small repeat loop. The first if statement checks to see if any results have been sent back. Next I check the time to see if there is something wrong with the server end of things. If ten seconds has passed since the load variables action then display an error message to the user.
If (i_items ne "null")
If (i_items = 0 )
Go to and Play (1)
Else
Go to and Stop ("setup")
End If
End If
If (GetTimer-loadTime > timeOut)
Go to and Stop ("timeout")
End If
The Setup So what exactly does the script send back?
The format of the output is crutial. So lets take a look at a sample: &i_items=3& &i01_id=233& &i01_name=jason krogh& &i01_email=knob@zincroe.com& &i01_url=http://www.zincroe.com& &i01_date=July 1, 2000& &i01_message=Your site depresses me& &i02_id=234& &i02_name=bob& &i02_email=bob@zincroe.com& &i02_url=http://www.zincroe.com& &i02_date=July 2, 2000& &i02_message=What's your problem???& &i03_id=235& &i03_name=jimbo& &i03_email=jimbo@zincroe.com& &i03_url=http://www.zincroe.com& &i03_date=July 3, 2000& &i03_message=Get a life man!& Every guestbook entry has six values: id, name, email, url, date and message. Normally values can be formatted all on one line. To make things more readable I have added an & at the start and end of each line. The data needs to be URL-encoded. Each name/value pair contains an "=" and is separated by an ampersand and each variable name has to be unique. The encoding scheme is important for special characters, especially if you need to use "=" or "&" within a value. The first value sent is i_items. This tells me exactly how many values to expect. The list of values all start with i01, i02, i03 etc. The two digit number is used to identify each item in the list and can therefore handle up to 99 items in one pass. These can be treated in the same way as arrays in flash. The Setup Once the data is loaded we move on to the setup frame. First we call on the build_list frame to populate the menu of entries on the left side of the screen. Then we choose the last one in that list to fill the display on the right side. Note that the variable 'current' contains the active selection and is used by the display script to fill in the details each entry.
Comment: Populate menu, Make last one current
Call ("build_list")
If (i_items<10)
Set Variable: "i_items" = "0"&i_items
End If
Set Variable: "current" = i_items
Call ("display")
Go to and Stop ("wait")
Building the Menu
Displaying the entries in flash can be as easy or as complicated as you want. I've taken the middle road in this case. I have a column of movie clips which can display the name of a single item. Note the instance names (i01, i02, etc...) on these. Each movie clip has an active and inactive state. When active it displays the entry name with a button which displays the details of that entry on the right side of the screen. This is the code which goes through the list of items and sets up each movie clip. Loop While (count New Entries The form for new entries is in the frame labelled 'add'. Notice that I've placed the form and the submit button together inside of a movie clip. The reason for this is that when submitting variables flash will send everything that is local to a particular timeline. By placing everything inside a movie clip I ensure that I'm not sending other, unwanted variables to the server. The submit button gathers the variables and sends them off to the server script. The first few lines check to see if the user included the http:// part of the url. If it's not there it adds to automatically. Then I reset the timer and the value of i_items for the loading sequence. The script is also designed to respond to save=1 by saving the information to the database as a new record and then returning a list of the latest entries with the new one included.
On (Release)
If (substring(url,1,7) ne "http://")
Set Variable: "url" = "http://"&url
End If
Set Variable: "/:i_items" = "null"
Set Variable: "/:loadTime" = GetTimer
Set Variable: "save" = "1"
Set Variable: "list" = /:list
Load Variables ("/cgi/guestbook.cgi", 0, vars=GET)
Begin Tell Target ("/")
Go to and Play ("loading")
End Tell Target
End On
That's not all
There are a number of other little things that went into the building of the guestbook. I will leave some of these for you to explore in the source file. And there are many, many extensions that can be made to the guestbook model. To summarize flash can:
|
||||
|
©2000 Ultrashock.com Inc.
- All rights reserved
|