Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Actionscript 3.0 and Zend AMF

This is a discussion on Actionscript 3.0 and Zend AMF within the Tutorials forums, part of the Flash English category; Zend is a PHP framework. Zend Amf permits communications between Flash and PHP without using XML. For example, to query ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 11-11-08, 05:02
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Actionscript 3.0 and Zend AMF

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:








__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 01-12-08, 20:40
Junior Member
 
Join Date: Nov 2008
Location: Tucson Az and San Diego Ca
Posts: 7
Rep Power: 0
ryanhungate is on a distinguished road
Re: Actionscript 3.0 and Zend AMF

Hi Flep, not that I really need this right away, but for some reason I am getting a:

netconnection failed...
I have my ZendAmf folder in the root...

I have my frameworks folder in the root... with the Zend in that.

I put my Comments.php file in the ZendAmf folder, and put my database info in it also...

I put my desired TABLE name to query in place of yours on the Comments.php file.

The flash file is also changed to the right folder, and also referencing the (description) catagory in SQL.

Thanks a bunch man, this site is so helpful, I wish I had money to donate right now! I will sooner or later :)

Ryan Hungate
__________________
Ryan Hungate
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-12-08, 10:10
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Actionscript 3.0 and Zend AMF

Hi,
you should try to use the PHP class you created with PHp only and see if it works.
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Tags
flash and php, Zend

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3.0 e Zend AMF Flep Articoli e tutorials 2 10-12-08 17:17


All times are GMT. The time now is 19:26.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap