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 use Macromedia Flash Actionscript together with PHP and a MySql database
to retrieve a list of mp3 files, place them in a combobox and when a user selects one of the mp3's
it will play and some if its information will be displayed.
When you have read some of my other Flash, PHP and MySql integration tutorials you might
have noticed codes often look a bit the same. When you start to get into the whole Flash and PHP integration, especially with the way i always set
things up, you will start to notice that you dont have to re-code lots of things all the time.
Its quite easy port code from file to file and adapt it a little to make it work in a new project too.
Thats true for the PHP code and the Flash Actionscript code and im glad it is because i wouldn't want
to re-code everything over and over all the time. I always have a little code library of my own scripts or
i take some code from my other projects, adapt it for a new project and voilá ... done. This saves you heaps
of time you really need in this game. Lets start by creating the database table first.
Creating the MySql database
We will create a MySql database that holds some
information on a few mp3 files. We will store a name, an id and a download link.
You can use the following SQL query to create the database for this tutorial.
You can insert this query directly into the MySql command terminal, but i almost never use
that personally.
I prefer to use a free 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 code
<?
We must start by defining a few variables for the database connection, the first one is the database host (usually localhost).
$server = "localhost";
The database user name.
$user = "database username here";
The database password.
$pass = "database password here";
The database name
$database = "database name here";
The database table name
$tableName = "database table name";
Now let us open the connection to the database with "mysql_connect".
The @ is a way to hide certain messages a query can output.
Some of the variables we defined earlier will be plugged in here to create
the connection.
.
$conn = @mysql_connect($server,$user,$pass);
Now we select our 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 mp3 file 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 would stay empty we would know we dont have any mp3 files in our database!
$total_rows = @mysql_num_rows($query);
Here we setup a counter variable. When we loop through the data,
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 table. Which in our case were: id, name, age, downloadLink.
So $id = $ourData["id"]; gets all the ids the mp3 files have and places them in a
variable called "$id".
$id = $ourData["id"];
This gets all the mp3 names and places them in a variable called "$name".
$name = $ourData["name"];
This gets all the download links and places them in a variable called "$downloadLink".
$downloadLink = $ourData["downloadLink"];
Now we have to increase our counter variable. The ++ means increase by one with
every iteration. So it becomes 1,2,3,4,5 etc....
$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: &mp3_data. Now have a look at this part &mp3_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: &mp3_data1 &mp3_data2 &mp3_data3 etc...
The next part which might look strange to you is this part =$id|$name|$downloadLink.
What we do there is we sort of pacl our variables into one master variable. So in our
case "mp3_data$counter" is filled with the variables we defined earlier in the code!
So an example of how this line of code could render in the loop could be:
&mp3_data1=1|loop01.mp3|mp3/loop01.mp3
&mp3_data2=678|loop02.mp3|mp3/loop02.mp3
hi guys i just make it run this is a php script program so you need to have a server installed on your computer to run it. make some modification on the path where your www forlder in php was.
thanks man nice program...
17-10-07:guest
Hi primevector, when are you fixing the script error, I also want to use your code :)
01-03-07:edd
no worries m8, got an assignment based on it for next would really help if u could fix this. thanks
28-02-07:primevector
Sorry about the script error, there seems to have been some changes with flash talking to php and i will look into it as soon as possible. Im hard pressed for time so it might not be very soon, but ill have a look @ it asap.
27-02-07:edd
how come there is script error, could some1 please help me out.
Comments
12 comment(s) found in 2 pages
viewing comment(s): 1 - 6
page: 1
1 | 2 |
08-01-08:iceman
hi guys i just make it run this is a php script program so you need to have a server installed on your computer to run it. make some modification on the path where your www forlder in php was. thanks man nice program...
17-10-07:guest
Hi primevector, when are you fixing the script error, I also want to use your code :)
01-03-07:edd
no worries m8, got an assignment based on it for next would really help if u could fix this. thanks
28-02-07:primevector
Sorry about the script error, there seems to have been some changes with flash talking to php and i will look into it as soon as possible. Im hard pressed for time so it might not be very soon, but ill have a look @ it asap.
27-02-07:edd
how come there is script error, could some1 please help me out.
27-12-06:guest
- Press here to view all available smilies!