ActionScript 3 and populating a DataGrid from a PHP script with data from a MySQL database.

Flash Actionscript3 DataGrid Component

ActionScript3 DataGrid Component being populated by a PHP script
ActionScript3 DataGrid Component being populated by a PHP script | Source

ActionScript 3 Datagrid Component populated by a PHP script.

I have to say I really struggled with this. I searched the forums, trawled the net, via Google of course, but there was no clear answer to my problem. I could find plenty of articles and forum posts on how to get data into the DataGrid, but it was all hard coded into the DataGrid component itself. There didn't seem to be anyone telling me how to get data from a MySQL database via a PHP script and populate the ActionScript3 DataGrid component.

So when I got it worked out, I put it into a tutorial so others can benefit from it.

I will share a little of the code that I use in the tutorial.

Firstly, I extract the data from the database via a simple PHP script.

Here is an example of one of the PHP scripts, I use to populate an Actionscript DataGrid component.

<?

/*This section points to the file that
contains the database connection info
by keeping it in a separate file if you
need to move the program to another
server or database you only need to
make the change in one file rather than
making the changes in several places.*/

I keep the database login information in a separate file so that when I have lots of PHP files all accessing the database if I need to change the database, I only need to alter the setting in one place, otherwise I would have to alter the login details in each PHP script.

This is an example of the host_file.ini

<?
$host = "localhost";
$user = "Database UserName";
$pass = "Database Password";
$db="DatabaseName";
?>
<?
//This includes the the login details
for the databse
include "host_file.ini";
mysql_connect($host,$user,$pass);
mysql_select_db($db);
//This is  the SQL querty - just a
simple select query.
$x="SELECT
`Customer_table`.`cust_id_num`,
`Customer_table`.`cust_title`,
`Customer_table`.`cust_firstName`,
`Customer_table`.`cust_lastName`,
`Customer_table`.`cust_address`,
`Customer_table`.`cust_city`,
`Customer_table`.`cust_areacode`,
`Customer_table`.`cust_telephone`,
`Customer_table`.`cust_notes`,
`Customer_table`.`cust_date`,
`Customer_table`.`cust_dob`
FROM
`Customer_table`
ORDER BY
`Customer_table`.`cust_lastName` ASC";
 
//PHP reads the mysql data into an
array
 
$xx = mysql_query("$x")or
die("problem!" . mysql_error());
list($cust_id_num,$cust_title,$cust_firstName,$cust_lastName,$cust_address,$cust_city,
$cust_areacode,$cust_telephone,$cust_notes,$cust_date,$cust_dob)
= mysql_fetch_row($xx);
 
 
/* The PHP code below formats the
database results into an array readable by the Flash
datagrid.*/
 
$i = 0;
$n = 0;
 
$results = mysql_query("$x");
$n= mysql_num_rows ($results);
while ($row = mysql_fetch_array
($results)) {
while (list ($key, $val) =
each ($row)) {
$r_string .= '&' . $key
. $i . '=' . stripslashes($val);
}
$i++;
 
}
 
echo "i=".$i;
echo "&n=".$n;
echo $r_string;
 
 
?>


The last lot of statements where it says the word “echo” this is where it sends the output to the Flash program and ActionScript takes over from there.

I found ActionScript 3 to be a lot more sensitive and quite different from its predecessor ActionScript 2.

I have put a lot of helpful tutorials on my website at interactivewebconcepts.net and most of them are free so it would be a good place to start if your new to Flash and ActionScript.



Comments 2 comments

gerry 5 years ago

Hi - good stuff but you haven't included the flash side of things. ie: receiving the variables from the php code.


raven 3 years ago

where is the code of the flash?

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working