Integrate Macromedia Flash, PHP and MySql and display a list of users - Part 2


posted: 28-10-05
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 14931
comments: 8
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 ?

In this tutorial we will repeat a lot of the code from part 1 of this tutorial. If you breezed through part 1 then you can skip all that code in this part. But i would really recommend reading and especially coding it yourself line by line again if you dont feel you already know everything about loading data into Flash with PHP from MySql.

This time we will add two new functions and a few extra lines. This new code will make this tutorial a lot more useful because we will setup a bit of interaction with the datagrid and i'll show you a great trick using objects to easilly display your Flash PHP MySql data throughout your whole swf.


Let's begin with the database (Exactly the same as in part 1!)

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 PHP Part (Exactly the same as in part 1!)

<?
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.


Amazing Webmaster Widgets





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

Comments

8 comment(s) found in 2 pages  
viewing comment(s): 1 - 6
page: 1 

1 | 2 |

22-06-07:guest

Hello! Good Site! Thanks you! nkpslgheonvws

01-04-07:UKguy

im gettinng script error when loading, have to abort script. Where is this forum that you mention PrimeVector, i dont see a link to it on this site

17-01-07:NnnBqsiUHX

Hi! Very nice site! Thanks you very much! epUlMqAbv3P

25-02-06:PrimeVector

Hi SithLord, please refer all questions to the forum. :)

25-02-06:SithLord

I had a slow running Flash Player when I first tried this out but figured out it was just I had too many entries in my database. But now when I run it nothing shows in the data grid.. I have triple checked the code and it all is right. it shows the status loading data...... but once that disappears the grid is blank.

21-12-05:PrimeVector

Thanks for your nice comment and pointing out the error. The tutorial was mostly aimed at the PHP - Actionscript integration, and i didnt want to overly complicate the code by setting up a trick to deal with behavior like that. Flash always has something we must deal with hehe..... The most important thing to learn from this tutorial combo is dealing with the PHP data, how its displayed is secondairy. But thanks again for the warning. :)



- Press here to view all available smilies!



Latest News

Amazing Webmaster Tool

If you run a MySpace or teens related site with visitors interested in GLITTERS you will need this great tool: Webmaster Glitter Tool It will generate backlinks at an amazing pace!

 

Useful Click To Clipboard Script

This is a nice little copy to clipboard script. You can find it here!

 

Google Pages

Create pages on google quick and easy with Google Pages!

 

My Funny Pictures Site

Have fun on this funny pictutures site i made, i hope youll enjoy it :) PixHumor

 

IS YOUR CHILD SAFE???

With this great program you can make sure your child will be a lot safer while browsing the Internet.

 

New Site

I created a fun site for MSN Messenger Stuff called MSN Freak. You can find the site here: Free Winks @ MSN Freak

 

Useful free unlocker tool

This tool is a very useful tool that runs in the background. Whenever you try to for example copy a folder and explorer sceams at you it wont do that, the barrier is lifted and you can continue to do what you wanted too. Download it here.

 

GOOGLE worldwide: Languages and countries

Useful page showing all the Google Search Engines of all countries. Check it out here.

 

Google Trends

Google trends enables us to see where people want certain things the most. You can enter a keyword and find out where people search for that most often! Check Google Trends

 

Google PageRank Checker

I personally enjoy this Google pagerank checker a lot: Google PageRank Checker

 

New tutorials

I added a new basic Flash tutorial showing how to create a simple motion tween. You can find it here: Basic Flash Motion Tween

 

2 new Flash tutorials

I added 2 new Flash tutorials to the tutorial selection. On deals with the if else conditional statement and the other deals with setting up a bit of color with actionscript. You can checkout the tutorials here: Actionscript and colors and If Else Conditionals.

 

Oh My!

Its mad times overhere, im helping promote and have created a few sites which has taken quite some time from me. site1: http://www.flashgamefreak.com, site2 http://www.freewinks.net, site3 http://www.smilieworx.com and more are to come this and next week. This has taken its toll on my time to write tutorials. I will make up for that as soon as possible. Thanks for your understanding.

 

Basic Flash Tutorials

I added 3 new basic Flash tutorials to the tutorial list. Dealing with layers, What are functions and one about The Registration Point.

 

A bit of a delay

I have had a very crazy time around here the last month, because of this some tutorials have not been written yet. This is a list of tutorial i still need to write and you can expect them in the very near future.

 

Advertise on FWR

If you want to advertise here then please contact me here. We can make a nice deal and you can get stats on them how often you would like.

 

2 new tutorials

2 new web design related tutorials were added ... more Flash tutorials are on their way but its a bit mad around here these last days ;)

 

View All News



In Focus

Play Frogger Now!
Play frogger with 1 click download of gamevance!
Play Airstrike 1941 online for free!
Play Airstrike 1941 online for free!
Play World War 2 Commando Free!
Play World War 2 Commando online for free!
Free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Play Aztec Quest online for free!
Play Aztec Quest online for free!
Get a Free Auto Insurance Quote
Free quotes from top insurance carriers!
Do you shop at Home Depot?
Get a ton of Home Depot coupons and gift card offers! Also if your looking to improve you home this is the place to be!
Make cash with Penny Stocks!
Make cash with Penny Stocks!
Play Swat 2 Now
Play the swat 2 game and shoot those bad guys!
Play Bubble Boomers
Pop the bubbles of the like colors for this very fun and addictive game for FREE!!!
Customize your Facebook with a theme
Customize the colors and music on your facebook profile page today!
Print Coupons from Home!
Print Grocery Coupons from your house! Instant Savings!
Get free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Stop identity theft in its tracks!
Protect My ID makes it easy for you!
Save up to 70% on auto insurance!
Do you qualify for 60-70% off your auto insurance rates? Find out right now! Request quotes 24 hours a day with our secure online form. It's super fast and easy. See what your rate is absolutely free!
Play Streetfighter Now!!!
Get all the hottest new games and all the old school games in one place!
Play Pac Man right here!
Get all the hottest new games and all the old school games in one place!
Play the old school Baseball Stars
Get all the hottest new games and all the old school games in one place!
Dont get your identity stolen!
we offer multiple layers of protection and are backed by experian. Start with only $1!
Share your Opinion, win $50,000?
Share your Opinion, win $50,000? Couldn't get any easier! It's free and fun!

Cool Stuff