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:
##create the user table
CREATE TABLE `users` (
`id` int(4) unsigned NOT NULL auto_increment,
`name` char(80) default NULL,
`email` char(100) default NULL,
`telephone` char(10) default NULL,
##insert some data in the database
INSERT INTO `users` VALUES (‘’,’John’,’john@example.org’,’1234567′);
INSERT INTO `users` VALUES (‘’,’Henry’,’henry@example.org’,’2234567′);
INSERT INTO `users` VALUES (‘’,’Carl’,’carl@example.org’,’3234567′);
INSERT INTO `users` VALUES (‘’,’Leo’,’leo@example.org’,’4234567′);
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.
< ?php class query{ var $host = "localhost"; var $user = 'root'; var $database = 'seubanco'; var $pass = 'suasenha'; function query(){ $this->methodTable = array(
“description”=>”populando nosso datagrid”,
$this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);

function get_user(){
$query = mysql_query(“SELECT * FROM users”);
if (!mysql_error()) {
return $query;
}else {
return mysql_error();

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.
//classes do Flash Remoting
import mx.remoting.Service;
import mx.services.Log;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.remoting.DataGlue;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.RecordSet;
// creating a connection with Flash Remoting to AMFPHP.
// Add in the address of you PHP Server, with the class:
var conexao = new Service(“http://localhost//flashservices/gateway.php”, new Log(), “query”, null, null);
// Calling the method get_user()
var pcs:PendingCall = conexao.get_user();
pcs.responder = new RelayResponder(this, “user_Result”, “user_Fault”);
function user_Result(res:ResultEvent) {
var grid = _root.createClassObject(mx.controls.DataGrid, “dg”, _root.getNextHighestDepth(), {_x:20, _y:20});
grid.setSize(400, 350);
grid.dataProvider = res.result;
function user_Fault(fault:FaultEvent) {

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:
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:
$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.



Macromedia Flash Remoting

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

Leave a Reply