ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


Introduction to Flex Using PHP
Pages: 1, 2, 3

Testing Flex Builder 2

Just to make sure that everything is working well, let's do a little "Hello World" test. Thankfully, Flex Builder has already created a starting MXML file in the new Flex Project. So, you can add a simple line of Flex code to the file, as shown in Figure 5.

Creating a simple
Figure 5. Creating a simple "Hello World" Flex application

If you want to copy and paste the code, here it is:

<mx:Label text="Hello World">
</mx:Label>

This code adds a single label element to the Flex application with the text "Hello World." When you click Run in the toolbar, Flex compiles the MXML code and brings up your web browser with the application running. Figure 6 shows the page on a Macintosh.

The
Figure 6. The "Hello World" application

Not too much to look at, but hey, what do you want from a "Hello World" application? At least you now know that everything is installed properly and running smoothly.

You've also seen a bit of MXML. Here is the complete fragment:

<mx:Application xmlns:mx="..." layout="absolute">
<mx:Label text="Hello World">
</mx:Label>
</mx:Application>

The <mx:Application> tag defines the application. Then, the <mx:Label> tag places a label in the layout of the application. The text property defines the string that will appear in the label. A label is just one of the bevy of controls that the Flex framework provides. You also get buttons, sliders, tabs, images, movies, and even charts and graphs.

Flex allows you to script the application by adding ActionScript code blocks either in the MXML file or as external ActionScript files, in much the same way JavaScript code can go in the web page or be externally sourced from a JavaScript file.

Building the Subscriber PHP Application

To build a Flex interface that talks to a PHP application, you first need a PHP application. Say you've decided to build an application that stores a list of subscribers, including their first names, last names, and email addresses. The first step in building the application is to figure out where to store the data. You do that in a MySQL database using a table that the MySQL database code shown in Listing 1 created.

Listing 1. db.sql

DROP TABLE IF EXISTS emails;

CREATE TABLE emails (
   first TEXT,
   last TEXT,
   email TEXT
);

To get that table into MySQL, you must first create the database, then run the following db.sql code:

% mysqladmin create flex_php
% mysql flex_php < db.sql

To add some example data, run the following script from the command line:

% mysql flex_php
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 28 to server version: 4.0.17-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> INSERT INTO emails VALUES ('jack','herrington','jack_d_herrington@pobox.com' );
Query OK, 1 row affected (0.07 sec)

mysql> quit

You can use whatever values you like.

Then, create a list.php script that reads the data from the database table and exports it as XML. Listing 2 shows this PHP file.

Listing 2. list.php

<?php
header('Content-Type: text/xml');
?>
<subscribers>
<?php
require_once("DB.php");

$dsn = 'mysql://root@localhost/flex_php';
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }

$res = $db->query( "SELECT first, last, email FROM emails" );
while( $res->fetchInto( $row ) )
{
?>
<subscriber>
<first><?php echo( $row[0] ); ?></first>
<last><?php echo( $row[1] ); ?></last>
<email><?php echo( $row[2] ); ?></email>
</subscriber>
<?php
}
?>
</subscribers>

First, this script uses the header() function to tell the web server that the return is going to be XML. It then connects to the web server using the DB module available from PEAR (and installed by default with most installations). After the script connects to the database, it runs a simple Structured Query Language (SQL) query to collect all the records, then uses a while loop to write them out as XML tags.

Running the script on the command line produces the following result:

% php list.php
<subscribers>
<subscriber>
<first>jack</first>
<last>herrington</last>
<email>jack_d_herrington@pobox.com</email>
</subscriber>
</subscribers>
%

You now have a script that shows the contents of your database as XML. You can connect that to your Flex application easily. But you also need a way to add records to the database. For that, a separate addform.php script (Listing 3) is required.

Pages: 1, 2, 3

Next Pagearrow





Sponsored by: