Zend is a PHP framework.
Zend Amf permits communications between Flash and PHP without using XML.
For example, to query a DataBase by Flash we need to call a PHP script that executes the query at DB and returns an XML output.
At that point only Flash can display the data it got from DB.
We bypass XML by using Zend instead !
In this case PHP executes the query at DB and it returns the values ( i.e. an Array ) to Flash directly .
How ?
Using the NetConnection class of Actionscript 3.0 !
This tutorial will show a brief sample on how to query a DataBase with Actionscript 3.0 and Zend.
We can use both Flash CS3 or Flash CS4.
Step 1
Download Zend framework ( ZIP version 1.7.0 ).
Requirements:
- PHP 5.1.4 or more
Step 2
Be sure to have a database with at least one table to be queried.
I personally use the WordPress database of this blog and with Flash I retrieve the names of the authors of all the comments on blog.
Step 3
Create a new folder in the root server: httpdocs (or public_html, it depends on the server).
I created a folder named "frameworks".
Load inside "frameworks": library->Zend folder that I find once unpacked the ZIP.
I create a folder named "ZendAmf" in the root server again.
Step 4
Create index.php file and load it into ZendAmf folder :
PHP Code:
<?PHP
error_reporting ( E_ALL | E_STRICT );
ini_set ( "display_errors" , "on" );
ini_set ( "include_path" , ini_get ( "include_path" ) . ":../frameworks" );
require_once "Zend/Amf/Server.php" ;
require_once "Comments.php" ;
$server =new Zend_Amf_Server ();
$server -> setClass ( "Comments" );
echo( $server -> handle ());
?>
In the first row I enable the notice errors.
ini_set sets the value of the given configuration option.
The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.
So I included the path "frameworks".
The require_once() statement includes and evaluates the specified file during the execution of the script.
This is a behavior similar to the require() statement, with the only difference being that if the code from a file has already been included, it will not be included again.
So I say to PHP to include the files Server.php ( inside of Zend/Amf/ folder ) and Comments.php ( we create it at step 5 of this tutorial ).
I instance Zend using new Zend_Amf_Server() method, also instance the Comments class and execute the Zend handle() method.
Step 5
Create a PHP class named Comments.php
and load it into ZendAmf folder :
PHP Code:
<?php
class Comments
{
public function __construct ()
{
mysql_connect ( "localhost" , "username" , "password" );
mysql_select_db ( "database" );
}
public function getAuthors ()
{
$result = mysql_query ( "SELECT * FROM wp_comments" );
$t = array();
while( $row = mysql_fetch_assoc ( $result ))
{
array_push ( $t , $row );
}
return $t ;
}
}
?>
As we can see, this class connects to the DB in its contructor's function.
Then a method (getAuthors) that performs the query to the DB, makes a push into Array of each line of the table " wp_comments " and returns the Array.
We are going to get this Array with Flash.
Step 6
Open Flash.
Create a new FLA and save it as "zendamf.fla" .
Inside of its library I have a MovieClip that will be used as preloader with "Wheel" Linkage.
Drag a TextArea component on the stage and give it instance name of "my_ta";
Now open the actions panel and write:
Code:
var nc:NetConnection=new NetConnection();
nc.connect("http://url della cartella ZendAmf/");
var res:Responder=new Responder(onResult,onError);
my_ta.visible=false;
var wheel_mc:Wheel=new Wheel();
wheel_mc.width=wheel_mc.height=40;
wheel_mc.x=stage.stageWidth/2-wheel_mc.width/2;
wheel_mc.y=stage.stageHeight/2-wheel_mc.height/2;
addChild(wheel_mc);
nc.call("Comments.getAuthors",res);
function onResult(e:Object):void
{
for(var i:int=0;i < e.length;i++)
{
my_ta.appendText((i+1)+" - "+e[i].comment_author+"\n");
}
wheel_mc.stop();
removeChild(wheel_mc);
my_ta.visible=true;
}
function onError(e:Object):void
{
wheel_mc.stop();
removeChild(wheel_mc);
my_ta.text=e.toString();
my_ta.visible=true;
}
Step 7
Analizing Actionscript.
Create a new NetConnection instance
var nc:NetConnection=new NetConnection();
and connect it at ZendAmf folder in which I have index.php we just created
nc.connect("http://www.flepstudio.org/ZendAmf/");
Create a new Responder instance that will call onResult function when all the PHP data will be loaded and onError function if it gets into troubles.
var res:Responder=new Responder(onResult,onError);
set invisible the TextArea
my_ta.visible=false;
Adding the preloader
var wheel_mc:Wheel=new Wheel();
wheel_mc.width=wheel_mc.height=40;
wheel_mc.x=stage.stageWidth/2-wheel_mc.width/2;
wheel_mc.y=stage.stageHeight/2-wheel_mc.height/2;
addChild(wheel_mc);
Say to NetConnection instance to call getAuthors() method of Comments.php class
nc.call("Comments.getAuthors",res);
This function has been caaled when all the PHP data has been loaded.
The nice thing is that PHP returns an Object !
Create a loop to get the authors and assign them as text to TextArea.
Note that I call comment_author. It is tha name of the table's row I am querying ! This is fine !
So, the names of the rows of the table become properties of the Object that PHP returns!
function onResult(e:Object):void
{
for(var i:int=0;i<e.length;i++)
{
my_ta.appendText((i+1)+" - "+e[i].comment_author+"\n");
}
remove the preloader
wheel_mc.stop();
removeChild(wheel_mc);
set visible the TextArea
my_ta.visible=true;
}
This function has been called if Flash or PHP get into errors during the process.
function onError(e:Object):void
{
wheel_mc.stop();
removeChild(wheel_mc);
my_ta.text=e.toString();
my_ta.visible=true;
}
Here is the result: