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:
[sql]
##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,
PRIMARY KEY (`id`)
) ENGINE=MyISAM ;
##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′);
[/sql]
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]
< ?php
class query{
var $host = "localhost";
var $user = 'root';
var $database = 'seubanco';
var $pass = 'suasenha';
function query(){
$this->methodTable = array(
“get_user”=>array(
“description”=>”populando nosso datagrid”,
“access”=>”remote”
)
);
$this->conexao = mysql_pconnect($this->host,$this->user,$this->pass);
mysql_select_db($this->database);
}
function get_user(){
$query = mysql_query(“SELECT * FROM users”);
if (!mysql_error()) {
return $query;
}else {
return mysql_error();
}
}
}
?>
[/php]
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.
[as]
//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) {
trace(fault.fault.faultstring);
}
[/as]
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:
[as]
var grid = _root.createClassObject(mx.controls.DataGrid, “dg”, _root.getNextHighestDepth(), {_x:20, _y:20});
[/as]
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:
[php]
$query = mysql_query(“SELECT name,email FROM users”);
[/php]
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
This is much easier with Flex than it is with Flash, especially for PHP developers. Check out the PHP RIA SDK by Adobe at http://code.google.com/p/adobe-php-sdk/ for samples and code on how to use Flex and PHP together.
Mike
!!! It is class to itself