Integrating Flex & PHP - An Introductory Tutorial
Viewing the Departments
In order to add new employees, the Flash application running in the browser needs access to the database-stored list of departments (see the drop-down menu in the next image). To achieve this, one PHP script will send out the department names and IDs as XML data. I call this script departments_xml.php (you can find it in the downloadable source code). The PHP code is short and reasonably documented, so I’ll just highlight a couple of aspects.
First, as this PHP script returns XML data, it must send out the proper Content-type header:
header("Content-type: text/xml");
Then the PHP script prints the root XML element, which can have any name:
echo "<departments>\n";
Finally, the script selects every department from the database and prints each out as a record:
echo "<department>
<id>{$row['department_id']}</id>
<name>{$row['department']}</name>
</department>\n";
Make note of the labels given to the elements, as they’ll be referred to in Flex. (Also, I’m using the Improved MySQL extensions, which assumes you’re using PHP 5 and MySQL 4.1 or greater; if not, change the code to the older MySQL functions.)
Viewing the Employees
The Flash application contains a DataGrid that lists every employee (see the next image). This is done using another PHP script that also sends out XML, just like departments_xml.php. This script is called employees_xml.php, and it works just like the previous script. The query here does use a join to also retrieve each employee’s department name. The outputted XML record for each employee is created by these lines:
echo "<employee>
<first_name>{$row['first_name']}</first_name>
<last_name>{$row['last_name']}</last_name>
<department>{$row['department']}</department>
</employee>\n";
Again, note the specific element names.
| Attachment | Size |
|---|---|
| leu_flex_php_1.1.png | 69.58 KB |
| leu_flex_php_1.2.png | 10.2 KB |
| leu_flex_php_1.3.png | 14.37 KB |
| leu_flex_php_1.4.png | 13.05 KB |
| leu_flex_php_1.5.png | 9.63 KB |
| leu_flex_php_1.6.png | 7.71 KB |
| larry_ullman_flex_php1_code.zip | 382.27 KB |
- Login or register to post comments
- 17139 reads
- Printer-friendly version
(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 - 6:34pm
LarryUllman replied on Sun, 2009/05/31 - 9:10pm
in response to: dazweeja
dengiz replied on Sun, 2009/06/07 - 7: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 - 1:32pm
kalchuka replied on Sat, 2009/06/20 - 6:42am
LarryUllman replied on Mon, 2009/06/22 - 10:12am
danieldourado_2 replied on Tue, 2009/07/14 - 5:02pm