You are on page 1of 5

Configuring the MySQL database and PHP to access it as in all applications of th

e database, the first thing to do is define the data structure. In this case, it
created a new MySQL table with the following fields: * contact ID, smallint, pr
imary key, auto_increment * lastName, firstName * TINYTEXT TINYTEXT * phone, * e
mail TINYTEXT TINYTEXT * picFile TINYTEXT * caption, TINYTEXT Each field was fil
led manually, with instructions such as: insert into contacts values (NULL, 'And
erson', 'Lois', '301-424-5555 ',' lois@yahoo.com ',' lois.jpg ',' Lois , 2001 ')
This is a small table. In a larger table, it would make more sense to fill the
data by reading a text file with the necessary information. To make sure that th
e table can be accessed through PHP, a test was done using the following sample
code: <? Php mysql_connect ("servername", "username", "password") mysql_select_d
b ("databasename") ; $ qr = mysql_query ("SELECT * FROM contacts"); $ nrows = my
sql_num_rows ($ qr); for ($ i = 0, $ i <$ nrows; $ i + +) ($ row = mysql_fetch_a
rray ($ qr); echo $ row ['lastName'].",. "$ row [' firstName']."< br>" echo "."
$ row ['phone'].. "$ row ['email']." <br> "echo". "$ row ['picFile'].":." $ row
[' caption']."< br> <br> ";)> Setup Macromedia Flash movie to display text Macro
media Flash movies, you must have one or more text fields. You can dynamically c
reate a field through TextFiled create or establish a field in the film, give it
an instance name and use it. My choice was the latter. The main text field has
the instance name "content" (located in the layer "text field & scrollbar" from
FLA) and is configured for multiple rows (multirow), by selecting the option "Re
nder text as HTML (text processing as HTML), so you can insert links. Addition o
f the scroll bar Because all records of addresses that start with the same lette
r will be placed in the same field, it is necessary that the field is scrollable
if there are more records than will fit on the screen. To
do this, open the Components panel and drag a copy of the scroll bar for the tex
t field. It automatically falls into place and is associated with the instance "
content." It is not easy? Setting the movie clip to contain a JPEG is also neces
sary to add something in the film to contain the JPEG to be uploaded. To do this
, create a movie clip as allocator space (layer 'pic, caption "from FLA) and giv
e it the instance name" picHolder. In the same layer, add another textbox (multi
line, but not HTML) with the instance name "caption." Ds configuration tabs as b
uttons in movie clips. Like all the guides have the exact same function, but sli
ghtly different appearance, one can use the new Macromedia Flash MX, buttons in
movie clips. In other words, it creates a template guide that is a movie clip, i
t copies itself to as many guides as necessary, and sets the code to put the rig
ht letter and attach the handle on the correct tab, so This responds to a mouse
click. Each guide in the movie clip contains a text field called "letter". Each
guide in the movie clip also contains a function onRelease designated to it, whi
ch inform what should be done when you click on a given movie clip. To perform t
he right action, however, is necessary to know what tab is being accessed. For t
hat to happen, it is necessary to appoint the guides on movie clips so that you
can figure out which one is being accessed. The guide is called the instance "ta
ba," the guide B, "Tabb", etc.. Bearing in mind this information, let us examine
the code (in Table 1 of main movie) that sets up the three tabs correctly (so t
hat display the correct letter and are implemented correctly by clicking on them
): for (var i = 65; i <= 67; i + +) (/ / display the correct letter in the guide
this ["tab" + chr (i)]. letter.text = chr (i) / / if this tab is clicked this [
"tab "+ chr (i)]. onRelease = function () (/ / the character chr (i) can not be
used here - you must use the name mc / / because i will not be set when the even
t actually happens c.thisLetter this._name.substr = (3,1) = bigLetter.text c.thi
sLetter; content.htmlText = "Loading date is" + c.thisLetter / / the scope of th
is function is a main timeline, so you can refer directly ac c.sendAndLoad (flas
hmx_dbPassAndReturnString.php "c," POST ");)) Assign each letter, A through C, t
he text field" letter "of the tab. Likewise, assign each letter a routine that d
oes the following, by clicking on the tab: • Place the letter associated with th
e selected tab in the property "thisLetter" of the object c, a LoadVars object t
o be discussed at the next page.
• • •
Displays the font selected as background image in your address book. Informs the
user that the program is getting the information from the database. Initiates t
he transfer using the LoadVars object c to transfer the data.
We must not forget that all this is not being done on the main movie, they are c
onfigured to process instructions when clicking a tab. Then you must configure t
he LoadVars object itself, as well as the actions to be implemented when the dat
a is returned from the PHP script. The next section explains how.
LoadVars objects transfer data between server side scripts - PHP, ASP, CF, JSP o
r Perl - and Macromedia Flash MX movies. The properties of the LoadVars object i
s sent to the script as variables, the method POST or GET. All variables returne
d from the script are available in Macromedia Flash as properties of the LoadVar
s object specified in the charging method. Transferring Data with LoadVars Use L
oadVars.send format = loadVars.send (url, target, method) / / where all elements
are expressions. In the sample, send the letter of the selected tab so that the
PHP script can use it to query the database. Therefore, you must create a LoadV
ars object with one of its properties attributed to that letter. Before examinin
g the sample code, present a case in which a value is assigned to a property of
a LoadVars object newly created and sent to a PHP script:
var c = new LoadVars (); c.thisLetter = "A"; c.send (dbquery.php "," _self "," P
OST ");
The script reads the property as a variable POSTed, and uses it to query the dat
abase and display the information found:
<? Php mysql_connect ("servername", "username", "password") mysql_select_db ("db
name"); $ qr = mysql_query ("SELECT * FROM contacts WHERE LEFT (lastName, 1) = '
". $ HTTP_POST_VARS [' thisLetter']."'"); / / display the line of visitors (1) $
nrows = mysql_num_rows ($ qr); for ($ i = 0, $ i <$ nrows; $ i + +) ($ row = my
sql_fetch_array ( $ qr); echo $ row ['lastName'].",. "$ row [' firstName']."< br
>" echo "." $ row ['phone'].. "$ row [' email']."< Br> "echo". "$ row ['picFile'
].":." $ row [' caption']."< br> <br> ";
)?>
This is the result that appears in the output window (the same window where the
Flash movie was playing, because we specify "_self" as the target of loadVars.se
nd):
Anderson, Lois 301-424-5555 lois@yahoo.com lois.jpg: Lois, 2001 Armand, Cheryl a
nd Joe 912-948-4444 armandcj@hh.com babysue.jpg: C and J's baby Sue, 9 days old
(etc )
How we use POST as a parameter of transmission, the variables had to be read wit
h PHP $ HTTP_POST_VARS. If you need to create the same query string (as sometime
s happens), it can be sent without specifying the method, which will cause it to
revert to the default GET, due to the query string attached.
var c = new LoadVars (); c.send (dbquery.php? thisLetter = A "," newWindow ");
The code in the file dbquery.php produces the same result in the output window o
f the code previously seen, but in a new document window (newWindow):
<? Php mysql_connect ("servername", "username", "password") mysql_select_db ("db
name"); $ qr = mysql_query ("SELECT * FROM contacts WHERE LEFT (lastName, 1) = '
". $ HTTP_GET_VARS [' thisLetter']."'"); / / display the line of visitors (1) $
nrows = mysql_num_rows ($ qr); for ($ i = 0, $ i <$ nrows; $ i + +) ($ row = mys
ql_fetch_array ( $ qr); echo $ row ['lastName'].",. "$ row [' firstName']."< br>
"; echo "." $ row ['phone'].. "$ row [' email']."< Br> "echo". "$ row ['picFile'
].":." $ row [' caption']."< br> <br> ";)>
When creating a Flash application for the server side, it is recommended to run
simple code, like the above code, using. Send to make sure that the variables ar
e passed as expected, before testing any application. Receipts with LoadVars.loa
d
format = loadVarsObject.load (url) where url is an expression now that you know
how to send variables to PHP script and cause them to be properly used in it, wh
at should you do to ensure that the information is returned to Macromedia Flash
? Before you begin using the functions of sending and receiving together, just l
ook at the receipt, with the charging method of the LoadVars object. Sending var
iables to Macromedia Flash by a script on the server side should always be in th
e form of a string var = value separated by & signs; example:
lastName = Anderson & phone = 301-424-5555
If this string is sent in response to a loading LoadVars, variables can be read
with Macromedia Flash as properties of the LoadVars object. But there is one mor
e thing that should be taken into account.€Obtaining information from a database
and its return to the Macromedia Flash are not instantaneous - it needs somethi
ng to tell Flash when the task is completed, so the film does not try to use var
iables before they are available (a problem common among novice users). Therefor
e, it is necessary to define a function that is executed when the data is return
ed, and assign that role to the onLoad property of the LoadVars object. In other
words, there must be a statement that when the variables have been received, th
e function must be performed. This can be done in two ways: with an anonymous fu
nction (also called a function literal):
var c = new LoadVars (); c.onLoad = function () (returnvals.text = "returned fro
m php: \ n \ n"; for (i in this) (returnvals.text + = i + "=" + this [i] + "\ n"
;));
or with a named function, which is then assigned to the onLoad property. This fu
nction should not be called - ie, do not put () after the function name:
showValues function () (returnvals.text = "returned from php: \ n \ n"; for (i i
n this) (returnvals.text + = i + "=" + this [i] + "\ n";)) var c = new LoadVars
(); c.onLoad = showValues;
In either of the two alternatives, refer to "this" inside the function call is e
quivalent to the LoadVars object by name. So, to request the data, you can use a
ny of the strings above code by adding the following line:
c.load (passvars.php ");
and in passvars.php this code ...
<? Php $ lastName = "Anderson", $ phone = "301-424-5555" echo "lastName =". $ La
stName. "& Phone =". $ Phone;?>
... The following results:
return php: phone = 301-424-5555 lastName = Anderson onLoad = [type Function]
The two variables that are either receiving phone (telephone) and lastName (last
name). onLoad also appears in the example, although not a variable, it is also
a property of the LoadVars object. Usually, we know that we expect variables, an
d tasks take into account only those variables, ignoring other properties of the
LoadVars object. You can also remove any superfluous properties before issuing
the load command (although onLoad is not superfluous and must always be a proper
ty of LoadVars to function properly). Sending and receiving simultaneously throu
gh LoadVars.sendAndLoad format = loadVarsObject.sendAndLoad (url, loadVarsObject
, method), where url and method are expressions, and it is not loadVarsObject In
the sample, we will send the selected letter (or the guide that was selected) a
nd send back all the records returned by the query. Thus, the routine PHP should
send back a string var = value of all variables, and the Macromedia Flash movie
will have to use the correct properties of the LoadVars object to display them
in the text field with scrolling on-film. The PHP script returns a string simila
r to the following:
N = 4 = & lastName0 Anderson & Lois firstName0 = & phone = 301-424-5555 &...& ca
ption3 = Lasty "
that contains values corresponding to the four records found in which the surnam
e begins with the letter A. Note that also added a variable at the beginning of
the sequence, to inform the Macromedia Flash how many records to expect, so that
you can set the correct cycle to read them all. The following is a partial samp
le PHP script:
$ Nrows = mysql_num_rows ($ qr); $ rString = "n =". $ Nrows; for ($ i = 0, $ i <
$ nrows; $ i + +) ($ row = mysql_fetch_array ($ qr); $ rString .= "& lastName".
"=".$ i. $ row ['lastName']. "& FirstName." I. "=".$ $ row ['firstName'];
$ RString .= "& phone". "=".$ I. $ row ['phone']."& email. "=".$ I. $ row [' ema
il ']; $ rString .= "& picFile. i. "=".$ $ row ['picFile']. "& Caption". "=".$ I
. $ row ['caption'];) echo $ rString;
We have already examined all the code necessary Macromedia Flash, now you only n
eed to define the function to be performed when the variables are returned (to r
ead and do something with those variables) and then issue the command sendAndLoa
d correct. Following is the code of the Macromedia Flash movie (¬ denotes a line
break):
showContent function () (var i; content.htmlText = "" for (i = 0; i <this.n; i +
+) (if (this ["picFile" + i] <> "") (+ content.htmlText = ¬ "<b>" + this ["firs
tName" + i] + "" + this ["lastName" + i] + ¬ "</ b>" + "<a href = 'asfunction: s
howjpg," + this ["picFile" + i] + ¬ "," + escape (this ["caption" + i]) + ¬ "'>
(<font color='#0000cc'> pic </ font>) ¬ </ a> <br> ";) else (content.htmlText +
= ¬" <b> "+ this [" firstName "+ i] +" "+ this [" lastName "+ i] + ¬" </ b> <br>
";) content.htmlText + =" "+ this [" phone "+ i] +" <br> "; content.htmlText ¬
+ =" <a href = "mailto:" + this ["email" + i] + "'>" ¬ + this ["email" + i] + "<
/ a> <br> <br>";)) / / Creating new LoadVars c for data transfer var c = new Loa
dVars () ; c.onLoad = showContent;
Remember that you need to run their own sendAndLoad corresponding to the selecte
d tab from within the routine onRelease, with the following code:
this ["tab" + chr (i)]. onRelease = function () (... c.sendAndLoad (flashmx_dbPa
ssAndReturnString.php "c," POST ");)
Now we are ready to send an A, B or C to PHP script, and do something with the v
ariables returned using the same c LoadVars object to send and receive. To under
stand the function code showContent, we should remember that it has equivalent w
ays of accessing the property phone3 object c.
c.phone3 and c ["phone" +3] and i 3 = c ["phone" + i] and i = 3 this ["phone" +
i] / / if the code is inside a function assigned to c
(For more information on this subject, see the Addressing Movieclips (Working wi
th movie clips) in my actionscript-toolbox.com site, which applies to all object
s in ActionScript, and not just film clips.) So , just take each property return
ed and display it as desired, in the text field created. format = asfunction asf
unction: functionname, param where param is an expression There are two other in
teresting points in this sample. One is the use of asfunction to call a function
within Macromedia Flash itself, the other is the direct loading of a JPEG in Ma
cromedia Flash. As you can see the code showContent above asfunction allows the
establishment of a link similar to HTML from a text field of Macromedia Flash to
another routine Macromedia Flash. In this case, it calls function showjpg and s
ends a parameter (can only send one) that contains the name of the JPEG file and
the caption, separated by a #. Oshowjpg will look like this:
showjpg function (paramString) (/ / for the individual parameters: / / params [0
] = filename of the image / / params [1] = caption var params = paramString.spli
t ("#"); picHolder.loadMovie ( params [0]); caption.text = unescape (params [1])
;)
First, it is necessary to divide the parameter sent in the two parameters you wa
nt, using the string function split to produce an array of two elements. The fir
st element now contains the name of the JPEG, the second caption. Upload JPEGs f
ormat = mcInstanceName.loadMovie (jpgfile) which is an expression jpgfile The ne
xt line
picHolder.loadMovie (params [0]);
gives instruction to load the image specified by file name stored in params [0]
in the movie clip picHolder. How picHolder was created with an upper left regist
ration point (0,0), and resize all pictures to match its size, the picture is lo
aded into the exact place, and need not do anything about it. After loading the
JPEG on your allocator space, complete the caption with the caption text sent. T
he unescape function is used to decode the URL value (note that we use the escap
e function to encode the value as the URL before sending it - otherwise, charact
ers like ":" and "," contained in the legend would not play properly) . That's a
ll. The LoadVars object sends data, the script uses PHP and sends the data back,
and the function showContents processes the data when they are received by Macr
omedia Flash.

You might also like