Rich Internet Applications: Introduction to Adobe Flex and PHP - Integrating Flex and PHP (
Page 2 of 4 )
OK, now that we have a good idea of what Adobe Flex is, let's see how we can build something. The first step is to download Flex Builder 2.0 from the Adobe labs site at http://labs.adobe.com. If you are familiar with Eclipse, and have it installed, you can install the plugin version of Flex Builder. If not, then you'll want to install the standalone version.
Once installed, we'll want to create a new Flex application. In this tutorial, we're going to keep the application simple. We will create an application that will read names and email addresses from a database and display them to the user. Our simple application will also allow users to add usernames and email addresses to the database.
To start, create the database that we need. I've called mine "sample," but you can call yours whatever you like. Next, create the table that will hold the user data. Here is the SQL structure for our table:
CREATE TABLE `users` (
`userid` int(10) unsigned NOT NULL auto_increment,
`username` varchar(255) collate latin1_general_ci NOT NULL,
`emailaddress` varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
COLLATE=latin1_general_ci AUTO_INCREMENT=3 ;
OK, now that we have our table, let's create the PHP script that will add users and export the XML that the Flex application will consume. It's relatively simple, only 25 lines of code or so. Note that the quote_smart function is a best practice, according to the PHP.Net website (http://www.php.net/manual/en/function.mysql-
real-escape-string.php).
<?php
Define( "DATABASE_SERVER", "localhost" );
Define( "DATABASE_USERNAME, "username" );
Define( "DATABASE_PASSWORD", "password" );
Define( "DATABASE_NAME", "sample" );
//connect to the database
$mysql = mysql_connect(DATABASE_SERVER, DATABASER_USERNAME,
DATABASE_PASSWORD);
mysql_select_db( DATABASE_NAME );
// Quote variable to make safe
function quote_smart($value)
{
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not integer
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
if( $_POST["emailaddress"] AND $_POST["username"])
{
//add the user
$Query = sprintf("INSERT INTO users VALUES ('', '%s',
'%s')", quote_smart($_POST["username"], quote_smart($_POST
["emailaddress"])";
$Result = mysql_query( $Query );
}
//return a list of all the users
$Query = "SELECT * from users";
$Result = mysql_query( $Query );
$Return = "<users>";
while ( $User = mysql_fetch_object( $Result ) )
{
$Return .= "<user><userid>".$User-
>userid."</userid><username>".$User-
>username."</username><emailaddress>".$User-
>emailaddress."</emailaddress></user>";
}
$Return .= "</users>";
mysql_free_result( $Result );
print ($Return)
?>
The PHP should be fairly self explanatory. The $_POST variable is populated with values from our Flex application, with two fields required: emailaddress and username. If both of those are filled out, then it adds the user to the database. After that, we return a list of users in XML format. Note that you cannot pass PHP variables to Flex applications directly, they must be encoded in XML first.
By abstracting the user interface from the data retrieval, it allows you to easily change how the data is displayed. For example, you could use this same PHP script to pass data to a mobile phone version of the same application. All you would need for that is to write the front end of the application; the back end PHP script would remain the same.