Integrate Macromedia Flash, PHP and MySql to display a list of users


posted: 22-10-05
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 28020
comments: 22
total pages: 5
current page: 1
page jump: 1 | 2 | 3 | 4 | 5

download source files


Live Example!

advice

Readers that dont have Flash 8 installed wont be able to view the source files. They were created with Flash 8. You can buy Macromedia Flash Pro 8 here, or just download a trial first. This is the latest and best version of Flash and we can do nothing else but recommend it to everybody because we absolutely love it. This new edition opens up a lot more of the magic that was already there, and it certainly is a new fresh wind for us Flash developers. Its a great investment for the future. Sorry for not having the source files available in all versions, if demand is high i will try to add more types of source files where needed?


What am i going to learn in this tutorial ?

We are going to setup a small Mysql database filled with a list of users. We then will use PHP to send the data from the database to Flash. In Flash the data will be received by a loadvars object and placed in a datagrid. In the next part we will setup more interaction with the database in the form of retrieving more specific data on a user. We will setup something like when we press on a cell in the datagrid, data on a specific user will be retrieved and displayed. This makes it a bit more useful.


Let's begin with the database.

We will create a database that holds some information on ficional users. This information is: name, age and country and all users have an id. You can use the following SQL query to create the database. You can insert this query directly into the MySql command terminal, but i almost never use that personally. I prefer to use a database administrator program called phpMyAdmin or something similar. Programs like that give you a visual representation of your database and you can manipulate it all you want. The best things is that its free and a lot of hosts offer it in their basic package. So go and check your host if you have access to phpMyAdmin or else just install it. Now download the query to create the database here! and run it to create the database.


The Affiliate Classroom: Step by Step Training!

The PHP Part

<?
First we will define a few variables, the first one is the database host (usually localhost)
$server = "localhost";
Your database user name
$user = "your database username here";
Your database password
$pass = "your password here";
Your database name
$database = "your databse name here";
The table name
$tableName = "tutorial001_users";
Here we open the connection with "mysql_connect" , the @ is a way to hide certain messages a query can output. Some of the variables we defined will be plugged in here.
$conn = @mysql_connect($server,$user,$pass);
Now we select the database with "mysql_select_db", again we use the @.
$database = @mysql_select_db($database,$conn);
With "mysql_query" we setup and execute our query. What this query does is select *, which means select everything, from the table we defined in variable "$tableName", the result will be ordered by the name of the user with "ORDER BY".
$query = @mysql_query("SELECT * FROM $tableName ORDER BY name");
With "mysql_num_rows" we get the number of rows retrieved by our query. We will place this number in a variable called "$total_rows". The value of this variable will be sent to Flash and it will play a very important roll there. It will tell us if we have data or not, because if e.g. this variable stays empty we know we dont have any users in our database!
$total_rows = @mysql_num_rows($query);
Here we setup a counter variable. When we go through the data our query retrieved, this counter will be increased. We will use its value in the final print to Flash.
$counter = 0;
With a while loop we loop through the data our query retrieved. The "mysql_fetch_array" actually gets that data and places it in an array called "$ourData".
while($ourData = @mysql_fetch_array($query)){
     We now can get the data from the $ourData array by calling the column names we had
     setup in the database. Which in this case were: id, name, age, country. so
     $id = $ourData["id"]; gets all the ids and placed them in a variable called "$id".
     $id = $ourData["id"];
     This gets all the user names and places it in a variable called "$name".
     $name = $ourData["name"];
     This gets all the user ages and places it in a variable called "$age".
     $age = $ourData["age"];
     This gets all the user countries and places it in a variable called "$country".
     $country = $ourData["country"];
     Now we increase our counter variable.
     $counter++;
     Now we will start really sending data to Flash. To enable Flash to read a variable we
     setup here, the variable need an & in front of it. You can see that in effect in
     this part of the code: &user_data. Now have a look at this part &user_data$counter
     and see how our counter is appended to this variable. Because we are in a loop it will
     render to something like this: &user_data1 &user_data2 &user_data3 etc...
     The next part which might look strange to you is this part =$id|$name|$age|$country.
     As you can see here this fills "user_data$counter" with the variables we defined above!
     So an example of how this whole line could render in this loop would be:
     &user_data1=1|user1|27|US
     &user_data2=7|user7|37|Netherlands
     print("&user_data$counter=$id|$name|$age|$country");
Close the while loop
}
Here we print an extra variable to Flash. Its the total variable and it tells us how many users were retrieved. and close PHP.
print("&total=$total_rows");
?>

On the next page you can find this PHP code again, but without the comments.

next page
page jump: 1 | 2 | 3 | 4 | 5
jump to comments

Comments

22 comment(s) found in 4 pages  
viewing comment(s): 1 - 6
page: 1 

1 | 2 | 3 | 4 |

04-12-07:parrudex

rocker

28-07-07:guest

©

21-07-07:Alan

`Hello

21-07-07:guest

Hello

19-05-07:birudu bavolilu

birudu bavolilu

19-05-07:birudu bavolilu

birudu bavolilu



- Press here to view all available smilies!





In Focus
Cool Stuff