Académique Documents
Professionnel Documents
Culture Documents
I
Gareth Downes-Powell
The Project
In this tutorial, we're going to create the Flash application shown below, for the fictional Jungle Pet Store.
The finished application will display an inventory of the animals on sale at the pet store, along with a picture and a short description of the particular animal. Customers can select from different type of animals, such as amphibians, snakes etc, and a list is automatically updated showing the various types of animals in that category. Clicking on a particular animal shows more details about that animal. The details of the animals in stock are held in a MySQL database, and are read using PHP. The Flash application contacts a PHP page sending the information required. The PHP page then reads the information from the database, and sends it back to Flash in XML format. The Flash application then reads the XML and uses it to display the data.
Requirements
MySQL PHP with the DOM extension enabled Flash MX Professional
Page 1 of 10
You can create this table by executing the following SQL. CREATE TABLE categories ( id int(11) NOT NULL auto_increment, category varchar(75) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;
Page 2 of 10
Again, you can create this table easily by running the SQL shown below: CREATE TABLE inventory ( id int(11) NOT NULL auto_increment, name varchar(100) NOT NULL default '', category varchar(75) NOT NULL default '', price varchar(10) NOT NULL default '', description varchar(250) NOT NULL default '', image varchar(200) NOT NULL default '', PRIMARY KEY (id) ) TYPE=MyISAM;
Page 3 of 10
INSERT INTO inventory VALUES (2, 'Grey Tree Frog', 'Amphibians', '45.00', 'The chameleon of the frog world, can change to almost any natural colour', '/images/greytreefrog.jpg'); INSERT INTO inventory VALUES (1, 'American Green Tree Frog', 'Amphibians', '39.99', 'Easy to keep, changes colour from yellow to dark brown', '/images/americantreefrog.jpg'); INSERT INTO inventory VALUES (3, 'Poison Frog', 'Amphibians', '74.99', 'Dendrobates Azureus - Stunning unnatural blue frogs, need some experience to keep them successfully', '/images/azureus.jpg'); INSERT INTO inventory VALUES (4, 'Garter Snake', 'Snakes', '25.99', 'Common snakes, easy to look after and keep', '/images/garter.jpg'); INSERT INTO inventory VALUES (5, 'Jungle Carpet Python', 'Snakes', '149.99', 'Medium sized pythons, growing to 6 or 7 foot. Good Feeders at all ages', '/images/junglecarpetpython.jpg'); INSERT INTO inventory VALUES (6, 'Cobra', 'Snakes', '249.99', 'Highly venemous snakes, experienced handlers only', '/images/cobra.jpg'); INSERT INTO inventory VALUES (7, 'Mexican Red Kneed Tarantula', 'Spiders', '45.99', 'Usually peaceful, however can release hairs which can cause blindness or rashes', '/images/mexicanredknee.jpg'); INSERT INTO inventory VALUES (8, 'Black Widow', 'Spiders', '99.99', 'Highly venemous spiders, for experienced collectors only', '/images./blackwidow.jpg'); INSERT INTO inventory VALUES (9, 'Mouse Spider', 'Spiders', '89.99', 'Mouse Spider native to Australia, often confused with funnel-web spiders', '/images/mousespider.jpg'); INSERT INTO inventory VALUES (10, 'Giant Snail', 'Insects', '9.99', 'Giant Snails, interesting pets with a life span of up to 20 years!', '/images/giantsnail.jpg'); INSERT INTO inventory VALUES (11, 'Crickets', 'Insects', '2.99', 'Sold in packs of 24. Interesting pets or ideal as food for other animals', '/images/cricket.jpg'); INSERT INTO inventory VALUES (12, 'Locusts', 'Insects', '4.99', 'Easy to keep, ideal for studying', 'images/locusts.jpg'); Now that we have filled our database with data, we can move on and start creating the PHP pages, which will read from the database, and send the data in XML format to the Flash application.
Page 4 of 10
Create a new PHP page in Dreamweaver MX, and delete all the automatically generated code so the page is blank. Create in a new directory called includes in the root directory of your website, and save the file in the includes directory as dbConnection.php. Next, in code view, add the code shown below to the page (this code assumes the PHP server and the MySQL server are on the same computer): <?php // Database Connection Parameters $dbHost = "localhost"; $dbName = "jungle"; $dbUser = "jungle"; $dbPass = "petstore"; ?> In the code above, we have the hostname of the MySQL server which is localhost if the PHP server and MySQL server are on the same computer. We also have the name of the database, and the username and password used to connect to it (that we defined at the start of the article). Save the page, close it and upload to your web server. Next job reading data from the categories table
Page 5 of 10
First, we add the dbConnection.php include file to the page, which will make the database connection variables available to us. Next, we open a connection to the database using the mysql_connect() command passing it the database server address, and the username and password needed to connect (from the include file). A link to the database connection is passed back which we store in $dbLink. If the database connection cannot be opened we stop the script using the die() command and display a message to the user. Next, we use the mysql_select_db() command to tell MySQL which database we want to work with. Again, if the command fails we use the die() command to stop the script as there's no point in continuing. We then create the SQL statement to select all the categories from the categories table, ordered in alphabetical order. The next step is to execute the SQL query using the mysql_query() command, passing it the query and the link to the open database connection. As usual, if the command fails we use the die() command to stop the script, displaying the error returned to the user with the mysql_error() command. We then use the mysql_num_rows() command to find the number of records returned by the query, passing it the link to the database result returned by the mysql_query() command. We then use a for loop, which runs once for every record returned, using the mysql_fetch_assoc() to read in a record at a time, in the form of an array, passing the command the link to the database results. We place each record into an array called $recordset, ready to transform into XML in the next section of code. Finally, we close the link to the open database connection using the mysql_close() command.
Page 6 of 10
If there are records available, then we new use the new_xmldoc() command to create a link to a new XML document, passing it "1.0" indicating that we want the XML document in version one format (which is standard). Next, we need to add an XML root element to the document, named categories, using the add_root() command. Now that we have a root node, we can start adding child nodes. We can do this by using the new_child() command, passing it the element name and the text to go between the elements. First, we add a child node which will act as a header for the list using new_child("category","Select Category"). In the XML document, this will appear as "<category>Select Category</category>". We then need to add a child element for each record held in $recordset. To do this, we use a foreach() loop to loop through each record in $recordset. We use the new_child() command, passing it the name of the tag, "category", and the data to go between the tags, which is the category from the database. Finally, now that all the data has been added to the XML document, we use the dumpmem() command to output the XML document. Save the page as before we move on we'll test it.
Page 7 of 10
Create a new PHP page, and delete all the code automatically added to the page by Dreamweaver MX so the page is completely blank. Save the page as inventory.php.
Page 8 of 10
by the query using the mysql_num_rows() command, passing it the result returned from the mysql_query() command. Finally, we use a for loop to read in each returned record into a variable called $recordset using the mysql_fetch_assoc() command. The last step is to close the open database connection as we no longer require it using the mysql_close() command.
Page 9 of 10
Summary
At this stage we have created a database and populated it with data about the animals on sale in the Jungle Pet Store. We have also created two pages, category.php, which retrieves the categories from the database and using the PHP DOM extension, transforms the data into an XML document, and inventory.php which pulls all records from the database corresponding to a certain category passed in the URL, and again returns this data as an XML document. In the next part of this tutorial we will build the Flash side of this tutorial, which takes the XML data generated by the PHP pages and displays it in real time to the user.
Copyright 2004 DMXzone.com All Rights Reserved To get more go to DMXzone.com
Page 10 of 10