Flexing with PHP

I have developed this example to understand how the data services and data providers work in Adobe Flex, and after I finished it I thought this would be a good mini-tutorial.

flex_php_1.png

I used PHP as the server side scripting language, but the same can be applied to any other language e.g. JSP/Serlvets, ASP, …etc.

First You have to create a simple mysql database “named flexdump in the example” that contains a single table called products:
create table products(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, product
VARCHAR(250) NOT NULL);

Flex code:

<mx:HTTPService id="addFormService" url="http://127.0.0.1:5050/flex1/add.php"
method="GET">
<mx:request xmlns="">
<product>
{product.text}
</product>
</mx:request>
</mx:HTTPService>
<mx:Form id="addForm" x="10" y="10">
<mx:FormHeading label="Add new products"/>
<mx:FormItem label="Product name:">
<mx:TextInput id="product"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Add" click="addFormService.send()"/>
</mx:FormItem>
</mx:Form>
<mx:HTTPService id="viewFormService" url="http://127.0.0.1:5050/flex1/ret.php" method="GET">
</mx:HTTPService>
<mx:Form id="viewForm" x="10" y="126">
<mx:FormHeading label="View products"/>
<mx:FormItem label="Products:">
<mx:List width="162" dataProvider="{viewFormService.lastResult.products.product}" />
</mx:FormItem>
<mx:FormItem>
<mx:Button label="View" click="viewFormService.send()"/>
</mx:FormItem>
</mx:Form>

PHP code:

//db.php
<?php
$db = mysql_connect("127.0.0.1:5051", "root", "") or die("erroorrr");
mysql_select_db("flexdump", $db) or die("erroorrr");
?>


//add.php
<?php
require("db.php");
$sql = "INSERT INTO products (product) VALUES ('" . $_GET['product'] . "');";
mysql_query($sql);
?>


//ret.php
<?php
require("db.php");
echo "<?xml version='1.0' encoding='ISO-8859-1' ?>\n";
echo "<products>\n";
$sql = "SELECT * FROM products";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
echo "<product>" . $row['product'] . "</product>\n";
}
echo "</products>";
?>

if there is any part you don’t understand feel free to contact me by leaving a comment..


  1. Anonymous

    I will bookmark your article for future use. It was very interesting and informative. Thank you.
    Have a nice day and continue working in the same way! ;)

  2. Amr Kamel

    THNX!

  3. nada ahmed

    hello
    i’m web developer using php & of course i’m new in adobe flex , can u recommend me a rich source to learn ?!

    thanx in advance

  4. Amr Kamel

    @Nada,

    Do you want to learn just Flex or learn Flex and how to integrate it with PHP?

    regards.

  5. Amr Kamel

    @Nada,

    Any way my recommendations are:

    1- You should learn Action Script 3.0, there is a nice book named “Action Script 3.0 Bible” (by the way this is an option but I believe the more you can code in pure Action Script the more productive and creative you will be in Adobe Flex/Air).

    2- To learn Adobe Flex and how to integrate with PHP, read this book “Foundation Flex for Developers, Data-Driven Applications with PHP, ASP.NET, ColdFusion, and LCDS”.

    3- If you just want to learn Adobe Flex, I highly recommend O’reilly’s book “Programming Flex 2″ although it talks about Adobe Flex ver. 2 (a little bit outdated) but it will give you a very good foundation which you can build on later.

    I hope that helped :)

  6. Nada Ahmed

    Mr. Amr Thank you for quick & useful reply.
    Wow. It’s a lot of books to learn just adobe flex?! But what about Action script, is it macromedia flash tool?
    I will follow your advice for while and excuse me for frequent ask.

    Wish you Happy Times with flex

  7. Amr Kamel

    @Nada,

    The Action Script part is optional, Action Script is a programming language used in Adobe Flash/Flex/Air “or may be more who knows :D ”. When you compile/run your Flash/Flex/Air it is converted/compiled to Action Script.

    And for learning Flex you have two ways, either:

    1- learn Adobe Flex only as a standalone GUI technology then after that learn how to integrate it with your server side technology (e.g. PHP, JEE, Rails …etc), you may read the book I mentioned in last comment “Programming Flex 2″.

    2- Learn Adobe Flex and more oriented towards how to integrate it with PHP, read this book “Foundation Flex for Developers, Data-Driven Applications with PHP, ASP.NET, ColdFusion, and LCDS”

    and its not macromedia any more its Adobe :D

    and of course you are welcomed any time.

    And I am not Mr. :D

    cheers.




Leave a Comment