Beginners Flash Tutorial – Working with MYSQL and PHP

October 30, 2008 by admin  
Filed under Flash Tutorials, PHP

High Quality – Flash Tutorial Videos – Taught by experts.
We also have extensive Adobe Flash Tutorials in high quality video format. These are ideal for beginners who need to master Flash quickly

Title / Free Demo : Adobe Flash CS3 Tutorial Videos
Author:
James Gonzalez
Duration:
11 Hours – Lessons: 125

Flash with PHP / MySQL

The code used in this tutorial is mainly ActionScript 2.0, with notes for using ActionScript 3.0 – basic knowledge of databases (particularly MySQL) and server-side scripting concepts is assumed, for users who require an a tutorial on Beginners PHP and MySQL we recomend the Beginners PHP Training Video

Given that Flash is used primarily on the Web, database driven components are a common requirement. In order to use data in this way within your Flash movies, you will need to use some sort of server-side scripting language, such as PHP. We use server-side scripting to communicate with a data source on the server, and Flash has some built-in capabilities for handling the data at the client-side.

To demonstrate, we will create a simple database-driven Flash movie using PHP and MySQL. This example will need to be deployed on a server with PHP and MySQL installed.

In MySQL, create a database called ‘flashtest’, with one table in it called ‘flashdata’; in the table create just one field (for simplicity – the details of the database are not important), name the field ‘flash_text’ and choose VARCHAR as the data type. Enter a few rows of data into the table, any strings of text will be fine.

Now we need PHP code to connect to and query your database. Create a file called get_data.php in the directory you intend to put your Flash file in – this will need to be on the same server as the MySQL database for the following code to work. In get_data.php enter the following code (replacing username and password with your own):

Select All

<?php

//connect to the local MySQL
mysql_connect("localhost", "username", "password");

//select your database
mysql_select_db("flashtest");

//query the database
$query="select * from flashdata";
$result=mysql_query($query);

//find out how many entries there are
$num=mysql_num_rows($result);

//write variable out - this is what the Flash document will retreive
echo "&num=".$num;

//keep count
$count=0;

//loop through and write the data into variables for the flash document
while($row=mysql_fetch_array($result))
{
   //write out each entry as a variable
    $entry=$row["flash_text"];
    echo "&t".$count."=".$entry;
    //increment the count variable
    $count++;
}

?>

Tip: you can test your PHP at this stage by fetching get_data.php in a Web browser – it should look something like this:
php test

Now to use the data within a Flash movie, create a new Flash document and save it on the server also. The Flash document is able to access the variables written out by your PHP script via the GET variable. Open the Actions panel for Layer 1 and enter the following code (replacing ‘filepath’ with the path to your PHP script):

Select All

      //load the variables into a MovieClip by calling the server-side script
var data_mc:MovieClip=_root.createEmptyMovieClip("data_mc", _root.getNextHighestDepth());
     //loadVariables parameters: url, movieclip, method
loadVariables("http://localhost/filepath/get_data.php", data_mc, "GET");

    //create the function for using the data
data_mc.onData=function()
{
    //get the total number of entries
    var numEntries:Number=data_mc.num;
      //loop through the entries
   for(var i:Number=0; i

Now test your movie, it should look something like this:
output

For Flash MX and above, you can alternatively use the LoadVars object with the following syntax:

Select All

//create LoadVars object
var lv:LoadVars=new LoadVars();
//load the data
lv.load("http://localhost/filepath/get_data.php");
//setup load function
lv.onLoad=function(success)
{
//get the data
var numEntries:Number=lv.num;
//otherwise syntax is the same as above
};

If you’re using ActionScript 3.0, the syntax will be slightly different; you need to use the URLRequest and URLLoader objects to load the data into Flash.

PHP Tutorials – Arrays Part 1

October 20, 2008 by admin  
Filed under PHP, Recent Tutorials

High Quality PHP Tutorial Videos – Taught by Experts
We also have extensive PHP Tutorials in high quality video format. These are ideal for beginners who need to master PHP quickly
Title / Free Demo :
Beginners PHP Tutorial Videos
Author:
Mike Morton
Duration:
6 Hours – Lessons: 86

PHP Arrays – Basics

This tutorial covers creating Arrays – Suitable for all versions of PHP

Introduction to the PHP Array

The array data structure provides a convenient way to store multiple values in a single variable. An Array uses pointers or index keys to map each value stored within it. The type of pointer, and the syntax for accessing it, varies depending upon the array type used. An array is permitted to store other arrays as values. Here is a look at both of PHP’s array types and how they are created and used.

Indexed Array
An indexed array uses integers as the key. The Array index begins at 0. PHP imposes no limit on the maximum number of elements an array may contain, so there is no maximum ending integer. Of course, array size is subject to the memory limitations established in the PHP.ini file as well as of the server where PHP is running.

Associative Array

Associative arrays use string values as keys.

Creating an Indexed Array

An array can be created simply by storing a value to a variable that uses the array structure.

Example:


Select All

$mystaff[0]=’Matthew’;

The above method creates an indexed array. This method is often used when the array will be built with values that can not be pre-determined or that will be created on the fly.

If the values of the array are known ahead of time, you could use this syntax:

Example:


Select All

$mystaff = array(0=>’Matthew’, 1=>’Mark’, 2=>’Luke’);

In the above example, the array has 3 elements, even though the last element’s key is the integer 2. New PHP programmers may forget the indexed array key structure is zero-based, hence the 3rd element in the above example is referanced by 2 and not 3.

Both indexed array creation methods are interchangeable. You do not have to use one method under any particular circumstance.

Displaying an Indexed Array

The value of an indexed array element is displayed by referencing the array element by its key.

Example:

Select All

echo "The names of my three staff members are:
$mystaff[0] . ", " . $mystaff[1] . ", " . $mystaff[2];

Give it a try, create a new document enter the following code


Select All

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Arrays</title>
</head>
<body>
<?php   $mystaff = array(0=>’Matthew’,   1=>’Mark’,   2=>’Luke’);
 <p>echo "The names of my three staff members are: ".   $mystaff[0] . ",
" . $mystaff[1] . ", " . $mystaff[2]; ?>
</body> </html>

>and save it as array-test.php, upload it to your web server and view it.

Creating an Associative Array

An associative array uses a string value as the key. Where an indexed array is typically used to store strings, an associative array is typically used to store numeric values. However, it is entirely up to you which array type you use. These are guidelines, not rules.

In this example below, we will create an associative array containing salary information for those three staff members we created in the indexed array example above.

Example

$salaries["Matthew"] = 65000;
$salaries["Mark"] = 75000;
$salaries["Luke"] = 100000;

Instead of declaring each array value on its own line, you could also use this syntax:


Select All

$salaries=array("Matthew"=>65000, "Mark"=>75000, "Luke"=>100000);

Displaying an Associative Array

Select All

echo "Matthew’s salary is: $" . $salaries["Matthew"] . "". "Mark’s salary is:
$" . $salaries["Mark"] . "".
"Luke’s salary is: $" . $salaries["Luke"];

Give it a try, delete the contents of your array-test.php page and replace it with the follow:

Select All

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//
EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Arrays</title>
</head>
<body>
<?php   $salaries=array("Matthew"=>65000,   "Mark"=>75000,
"Luke"=>100000); echo "Matthew’s salary is: $" . $salaries["Matthew"] . "".
 "Mark’s salary is: $" . $salaries["Mark"] . "".
  "Luke’s salary is: $" . $salaries["Luke"]; ?>
</body>   </html>

After saving your page, upload to your webserver and view. You know have a fundamental knowledge of creating and displaying Arrays in PHP.

Summary

Arrays are powerful tools, especially when used with the for and while functions. There are other PHP functions designed especially for working with arrays. They provide convenient methods for sorting, transversing, and manipulating arrays. We’ll look at these in next PHP Array tutorial.