Integrating Flex & PHP - An Introductory Tutorial

Ten years ago or so, the World Wide Web primarily meant static HTML. Horrendous JavaScript, trivial games, and frivolous splash pages represented the common attempts at browser-based pizzazz. But in the past couple of years, a decade’s worth of server-side dominance has ironically revitalized interest in the client. Today, the best sites are using Rich Internet Applications (RIAs), tied into the server, to provide the user with an experience closer to a desktop application. RIAs are most commonly built using either JavaScript or Flash in the browser, communicating with some technology on the server. In this article I discuss how to use Flash in the browser, created using Flex (Flash content can be created using other software as well), to interact with PHP on the server.

Flex is an open-source framework (open-source as of version 3.0) managed by Adobe. The framework makes it very easy to use common elements and functionality, which can then be tied together via ActionScript, a language quite similar to JavaScript. A completed Flex project is compiled as a SWF file (pronounced “swiff”) that can run in any Web browser that has installed the Flash plugin. You can develop Flex projects using a text editor and the free Software Development Kit (SDK) or you can save yourself oodles of time by using Adobe’s commercial (but Eclipse-based) Flex Builder IDE (you can download a free trial of it here).

For the specific example, I’ll develop part of a Web-based application for managing employees. The application has two primary features:

1. The ability to add new employees.
2. A display of all the employees in a table (or a DataGrid, in Flex parlance).

I’ve chosen to focus on these particular aspects of an online application because they represent the most common client-server interactions: posting data to the server and getting data from the server. In the end, I’ll be generating these files:

•    An HTML page that contains the Flash.
•    The SWF file itself.
•    One PHP script that receives form data and returns a text response (for adding new employees).
•    One PHP script that just returns XML (for showing all current employees).
•    One PHP script that just returns XML (for listing the departments).

You can download the source code and resulting files here.


Since the Flex side of things will require the three PHP scripts (and the backend database) in order to function, I’ll work through that list backwards.  

Creating the Database
For this project, I’m going with a fairly routine employees/departments model, practical yet easily understood. My design has two tables: employees and departments. There’s a many-to-one relationship between them, in that each employee can only be in one department but each department can have multiple employees. For the sake of simplicity, I’m not concerning myself with department heads and company hierarchy or even all the details that might be associated with an employee or department. After you read this article, it shouldn’t be hard for you to go back in and change the particulars per your needs.

The SQL commands for creating the tables are:

CREATE TABLE `departments` (
`department_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`department` VARCHAR(40) NOT NULL,
UNIQUE (`department`)
)
CREATE TABLE `employees` (
`employee_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`department_id` TINYINT UNSIGNED NOT NULL,
`first_name` VARCHAR(20) NOT NULL,
`last_name` VARCHAR(30) NOT NULL,
INDEX (`department_id`),
INDEX `name` (`last_name`, `first_name`)
)

To populate the departments table, run these INSERTs (add more departments, if you’d like):
INSERT INTO `departments` (`name`) VALUES ('Human Resources')
INSERT INTO `departments` (`name`) VALUES ('Accounting')
INSERT INTO `departments` (`name`) VALUES ('Marketing')



I’ll be using MySQL as the database application, but if you’re using another one, those same SQL92-compliant commands should work.

AttachmentSize
leu_flex_php_1.1.png69.58 KB
leu_flex_php_1.2.png10.2 KB
leu_flex_php_1.3.png14.37 KB
leu_flex_php_1.4.png13.05 KB
leu_flex_php_1.5.png9.63 KB
leu_flex_php_1.6.png7.71 KB
larry_ullman_flex_php1_code.zip382.27 KB
0
Average: 5 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

dazweeja replied on Thu, 2009/05/28 - 7:34pm

Although I realise this is an introductory tutorial, I think using XML is more error-prone and probably has the same level of difficulty as using AMFPHP or ZendAMF. Using AMF means lower bandwidth and removing the extra step of echoing out lines of XML. Much easier and safer to send an array of typed objects which means your client and server-side code can be much more closely aligned.

LarryUllman replied on Sun, 2009/05/31 - 10:10pm in response to: dazweeja

Thanks for your feedback. I agree that using AMF often makes sense. But as you point out, this is an introductory tutorial, so I wanted to limit the number of foreign concepts discussed. Plus, XML, which can be a pain to debug, is often a logical chioce, too. Moreover, XML is often a required format, like when working with third-party data feeds. But for those going further with their PHP-Flex development, looking into AMF is a logical step. Thanks again!

dengiz replied on Sun, 2009/06/07 - 8:34pm

thanks a lot for this great tutorial. That is exactly what I was searching for!

Just one question: departments_xml.php didn't function properly. I couldn't find why.

LarryUllman replied on Mon, 2009/06/15 - 2:32pm

You're quite welcome. Glad it was helpful and thanks for the nice words. As for the XML file, what happens if you run it directly in your browser? Does it return proper XML or an error? And do make sure you run it through a URL.

kalchuka replied on Sat, 2009/06/20 - 7:42am

thnks for this tutorial. please can you help explain to me how to make use of amfphp with flex. i ve read a lot of article but seem not to grab the thing. i already know how to do php

LarryUllman replied on Mon, 2009/06/22 - 11:12am

Thanks for the feedback. I'll try to put together something on amfphp+Flex and will post a URL once I've done so. It'll be a while, though, as I'm in the middle of some other projects right now.

danieldourado_2 replied on Tue, 2009/07/14 - 6:02pm

I really would like to thank you for this tutorial, Ive been searching for 2 days in order to find a tutorial with examples which i could understand, yours is the best ive found, thank you!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.