Populating a Datagrid using AMFPHP

In the Internet we have some examples about using AMFPHP to create RIA applications. In this article we are going to create a RIA application more elaborated than a “Hello World�?, and you can use it in you real world application.

You need knowledge about PHP, MySQL, Flash and Flash Remoting.

First, we create a simple database structure, only with name, e-mail and telephone number. Let’s create the table and insert some data:

  1. ##create the user table
  2.     CREATE TABLE `users` (
  3.     `id` int(4) unsigned NOT NULL auto_increment,
  4.     `name` char(80) default NULL,
  5.     `email` char(100) default NULL,
  6.     `telephone` char(10) default NULL,
  7.     PRIMARY KEY (`id`)
  8.     ) ENGINE=MyISAM ;
  9.     ##insert some data in the database
  10.     INSERT INTO `users` VALUES ('’,'John’,'john@example.org’,'1234567′);
  11.     INSERT INTO `users` VALUES ('’,'Henry’,'henry@example.org’,'2234567′);
  12.     INSERT INTO `users` VALUES ('’,'Carl’,'carl@example.org’,'3234567′);
  13.     INSERT INTO `users` VALUES ('’,'Leo’,'leo@example.org’,'4234567′);
  14.     INSERT INTO `users` VALUES ('’,'Wendel’,'wendel@example.org’,'5234567′);

We have our user table in the database, now we create a AMFPHP class that connect with our database and return data for Flash.

  1. < ?php
  2.      class query{
  3.           var $host = "localhost";
  4.           var $user = 'root';
  5.           var $database = 'seubanco';
  6.           var $pass = 'suasenha';
  7.           function query(){
  8.               $this->methodTable = array(
  9.               "get_user"=>array(
  10.               "description"=>"populando nosso datagrid",
  11.               "access"=>"remote"
  12.               )
  13.               );
  14.               $this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);
  15.               mysql_select_db($this->database);
  16.           }
  17.        
  18.           function get_user(){
  19.               $query = mysql_query("SELECT * FROM users");
  20.               if (!mysql_error()) {
  21.                   return $query;
  22.               }else {
  23.                   return mysql_error();
  24.               }
  25.           }
  26.   }
  27. ?>

At this point, all right, now let’s go create the Flash. Open a new document, we use only one layer and one frame for importing all necessary classes from Flash Remoting.

ActionScript

  1. //classes do Flash Remoting
  2. import mx.remoting.Service;
  3. import mx.services.Log;
  4. import mx.rpc.RelayResponder;
  5. import mx.rpc.FaultEvent;
  6. import mx.remoting.DataGlue;
  7. import mx.rpc.ResultEvent;
  8. import mx.remoting.PendingCall;
  9. import mx.remoting.RecordSet;
  10. // creating a connection with Flash Remoting to AMFPHP.
  11. // Add in the address of you PHP Server, with the class:
  12. var conexao = new Service("http://localhost//flashservices/gateway.php", new Log(), "query", null, null);
  13. // Calling the method get_user()
  14. var pcs:PendingCall = conexao.get_user();
  15. pcs.responder = new RelayResponder(this, "user_Result", "user_Fault");
  16. function user_Result(res:ResultEvent) {
  17.     var grid = _root.createClassObject(mx.controls.DataGrid, "dg", _root.getNextHighestDepth(), {_x:20, _y:20});
  18.     grid.setSize(400, 350);
  19.     grid.dataProvider = res.result;
  20. }
  21. function user_Fault(fault:FaultEvent) {
  22.     trace(fault.fault.faultstring);
  23. }

All finished, you may asking, and the DataGrid? Where is? They must be in the movie or not? The answer is No. Because she will be created dynamically by the following line:

ActionScript

  1. var grid = _root.createClassObject(mx.controls.DataGrid, "dg", _root.getNextHighestDepth(), {_x:20, _y:20});

The DataGrid will be created with instance name “dg�? and in the position x=20 and y=20. So, in the next step is determinated the DataGrid size with setSize().

The only one requisite is that the DataGrid must be in the movie library. “One moment, but if I want to show only some fields in the DataGrid?�? You can use two method for this, the first is using action script (hard method) and second is using the following method on AMFPHP class:

  1. $query = mysql_query("SELECT name,email FROM users");

In the next articles we show you how to implement a application to insert, edit and delete data in you database, and how to configure you layout and headers of the DataGrid.

References:

AMFPHP
http://www.amfphp.org/

Macromedia Flash Remoting
http://www.macromedia.com/devnet/flashremoting/

Translated by:
wendel – http://www.wendelmaques.com.br

Was this article helpful? feel free to make a donation and help keep the blog in the air
AMFPHP, Flash, MySQL, PHP

Leave a Reply