Greetings to all!
In this article, we will see how to combine flash with the now famous vBulletin.
We will see how to create a SWF, which through Actionscript 3.0 will call a PHP script querying a mySQL database.*
The PHP returns values to Flash which will then display the numbers of registered users and the nick of the last one registered.
If you understood the contents of the article, in which I explained how to use the new Actionscript 3.0 with PHP and what is a Document Class in Flash CS3 , you are ready to add a new Flash module to your forum.
As an example, many of us use a cms as a show site or home page and then install the forum in another folder.
With this tutorial, we will be able to create a SWF that will retrieve the data from your forum's database and add it to your site's home page.
As always, you can add an animation as you wish.* I try to keep things as simple as possible so to make it easier to understand to all of you... I create a FLA, save it as 'utenti.fla' and assign to it the size 150x80.
On stage, I create:
- a MovieClip named 'loading_mc' visible the time the PHP script returns an answer. I add to it a small blinking animation.
- a MovieClip named 'utenti_mc' with into it a text field containing 'utenti registrati'. I also add to it a small animation so it appears from the right and position itself to the centre.
- a MovieClip named 'ultimo_mc' equal to 'utenti_mc' changing only the text to 'ultimo utente registrato'.
- a dynamic text field named 'numero_txt' which will display the number of registered users
- a dynamic text field named 'ultimo_txt' which will display the nick of the last user registered
Let's pass to the Actionscript 3.0:
I create a Document class, an AS file saved as 'Utenti.as' which will call the PHP script to retrieve the total number of registered users, implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.utils.Timer;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.events.*;
public class Utenti extends MovieClip
{
private var num_utenti:int;
private var timer:Timer;
private var boo:Boolean=true;
public function Utenti()
{
init();
callServer();
}
private function init():void
{
stage.frameRate=31;
}
private function callServer():void
{
var richiesta:URLRequest=new URLRequest();
richiesta.url='http://www.flepstudio.org/test.php'cachebuster='+new Date().getTime();
var loader:URLLoader=new URLLoader();
addListeners(loader);
try
{
loader.load(richiesta);
}
catch (error:Error)
{
trace('Unable to load requested document.');
}
}
private function addListeners(d:IEventDispatcher):void
{
d.addEventListener(Event.COMPLETE,completato);
}
private function completato(e:Event):void
{
loading_mc.stop();
loading_mc.visible=false;
var loader:URLLoader=URLLoader(e.target);
var vars:URLVariables=new URLVariables(loader.data);
num_utenti=vars.utenti;
utenti_mc.play();
utenti_mc.addEventListener(Event.ENTER_FRAME,checking);
}
private function checking(e:Event):void
{
if(e.currentTarget.currentFrame==31)
{
e.currentTarget.removeEventListener(Event.ENTER_FRAME,checking);
display();
var utlimo:Ultimo=new Ultimo(this);
}
}
private function display():void
{
numero_txt.text=num_utenti.toString();
timer=new Timer(250,0);
timer.addEventListener(TimerEvent.TIMER,fade);
timer.start();
}
private function fade(t:TimerEvent):void
{
if(boo)
utenti_mc.alpha=.75;
else
utenti_mc.alpha=1;
boo=!boo;
}
}
}
I create a second class, an AS file saved as 'Ultimo.as' which will call the second PHP script to retrieve the nick of the last registered user, implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.events.*;
public class Ultimo extends MovieClip
{
private var Root:MovieClip;
private var ultimo_utente:String;
public function Ultimo(m:MovieClip)
{
Root=m;
Root.loading_mc.visible=true;
Root.loading_mc.y+=40;
Root.loading_mc.play();
callServer();
}
private function callServer():void
{
var richiesta:URLRequest=new URLRequest();
richiesta.url='http://www.flepstudio.org/ultimo_registrato.php';
var loader:URLLoader=new URLLoader();
addListeners(loader);
try
{
loader.load(richiesta);
}
catch (error:Error)
{
trace('Unable to load requested document.');
}
}
private function addListeners(d:IEventDispatcher):void
{
d.addEventListener(Event.COMPLETE,completato);
}
private function completato(e:Event):void
{
Root.loading_mc.stop();
Root.loading_mc.visible=false;
var loader:URLLoader=URLLoader(e.target);
var vars:URLVariables=new URLVariables(loader.data);
ultimo_utente=vars.ultimo;
Root.ultimo_mc.play();
Root.ultimo_mc.addEventListener(Event.ENTER_FRAME,checking);
function checking(e:Event):void
{
if(e.currentTarget.currentFrame==31)
{
e.currentTarget.removeEventListener(Event.ENTER_FRAME,checking);
Root.ultimo_txt.text=ultimo_utente;
}
}
}
}
}
Let us analyse the code
Utenti.as class
Properties:
a numerical variable to which I assign the value returned by the PHP script (number of users)
private var num_utenti:int;
a Timer for a small animation
private var timer:Timer;
a Boolean variable still needed for the animation
private var boo:Boolean=true;
Methods:
init();
I impost the frame rate
stage.frameRate=31;
Now I will start explaining the method completato() as the rest of the class as already been seen in how to call a PHP script with Flash CS3
completato();
I stop the animation of 'loading_mc' and make it invisible
loading_mc.stop();
loading_mc.visible=false;
I retrieve the data returned by the PHP script making a query to the DB
var loader:URLLoader=URLLoader(e.target);
var vars:URLVariables=new URLVariables(loader.data);
I assign the value of the PHP to the variable num_utenti
num_utenti=vars.utenti;
I start the timeline of 'utenti_mc'
utenti_mc.play();
I create an interval ENTER_FRAME which control the frame number of 'utenti_mc' calling the function checking()
utenti_mc.addEventListener(Event.ENTER_FRAME,check ing);
checking();
if the current frame is 31
if(e.currentTarget.currentFrame==31)
{
I stop the interval
e.currentTarget.removeEventListener(Event.ENTER_FR AME,checking);
I call the method display()
display();
I create an instance of the Ultimo class (explained next)
var utlimo:Ultimo=new Ultimo(this);
}
display();
I assign to the text field 'numero_txt' the value of the variable 'num_utenti'
numero_txt.text=num_utenti.toString();
I create a Timer which will call the function fade() every 250 cents of seconds
timer=new Timer(250,0);
timer.addEventListener(TimerEvent.TIMER,fade);
timer.start();
fade();
if the variable 'boo' is true
if(boo)
I assign an alpha to 'utenti_mc' equal to 0,75
utenti_mc.alpha=.75;
if it is instead false
else
I assign an alpha to 'utenti_mc' equal to 1
utenti_mc.alpha=1;
I invert the value of 'boo'
boo=!boo;
Ultimo.as class
Properties:
a MovieClip variable to which I assign the root from the Utenti.as so that I can recall the MovieClip and text field created in utenti.fla
private var Root:MovieClip;
a string variable into which I insert the nick of the last registered user returned by the PHP script
private var ultimo_utente:String;
As we have already seen in other examples, the building class is the first one to be executed.
In fact, when we created an instance of Ultimo.as in Utenti.as, we have in the Ultimo.as the following code:
I assign to the variable 'Root' the root so that I can recall the MovieClip placed on stage
Root=m;
I make 'loading_mc' invisible
Root.loading_mc.visible=true;
I move its y of 40 pixels
Root.loading_mc.y+=40;
I start its timeline
Root.loading_mc.play();
Now we can pass to the method completato()
I stop the timeline of 'loading_mc' and make it invisible
Root.loading_mc.stop();
Root.loading_mc.visible=false;
I retrieve the data returned by the PHP script, a string containing the nick of the last registered user
var loader:URLLoader=URLLoader(e.target);
var vars:URLVariables=new URLVariables(loader.data);
I assign the data to the variable 'ultimo_utente'
ultimo_utente=vars.ultimo;
I start the timeline of 'ultimo_mc'
Root.ultimo_mc.play();
I create an interval ENTER_FRAME which control the frame number of 'ultimo_mc' calling the function checking()
Root.ultimo_mc.addEventListener(Event.ENTER_FRAME, checking);
function checking(e:Event):void
{
If the current frame is equal to 31
if(e.currentTarget.currentFrame==31)
{
I stop the interval
e.currentTarget.removeEventListener(Event.ENTER_FR AME,checking);
I assign the text to the text field 'ultimo_txt'
Root.ultimo_txt.text=ultimo_utente;
}
}
The PHP script used:
utenti.php
PHP Code:
<'php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$dbhost = 'localhost';
$dbuser = 'db_username';
$dbpass = 'db_password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'db_name';
mysql_select_db($dbname);
$query ="SELECT * FROM `vb_user`";
$db_query = mysql_query($query) or die (mysql_error());
$count = mysql_num_rows($db_query);
echo 'utenti='.$count;
'>
ultimo.php
PHP Code:
<'php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$dbhost = 'localhost';
$dbuser = 'db_username';
$dbpass = 'db_password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'db_name';
mysql_select_db($dbname);
$query="SELECT * FROM vb_user";
$result=mysql_query($query) or die (mysql_error());
$max=mysql_numrows($result);
$username=mysql_result($result,$max-1,"username");
echo 'ultimo='.$username;
'>
You only need to change:
- the database's user name, password and name
- the url to the PHP script in the Utenti class (line42) and Ultimo class (line38)
- change the permission of the two PHP file to 777 if needed
This is the result, using the data of FlepStudio's vbulletin:
Source files:
Bookmarks