Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Create a catalog in flash

1. Introduction

1.1 What we will do in this tutorial

In this tutorial, we will see how we can create a "little" product's catalog
using Flash, Php and MySql.

We'll have three files:

- config.inc.php, that contains the informations for the connection at the database
- Select.php, that select the product's dates from the database, and return it in a format for Flash
- Catalog.swf, that's the catalog the user will see :)

 

2. Start Up

2.1 Create the configuration file & the db table

We start from the database creation, than we'll see the php files
1) Well, we create our database. In this sample, I've called the database "catalog" (original name :D)

In our database, we must create a table (products) with some fields...use this query to create the table in your Database

CREATE TABLE products (
  id int(20) NOT NULL auto_increment,
  name varchar(200) NOT NULL default '',
  quantity int(5) NOT NULL default '0',
  cost int(5) NOT NULL default '0',
  description tinytext NOT NULL,
  colors tinytext NOT NULL,
  image varchar(200) NOT NULL default '',
  PRIMARY KEY (id)
  )


Well done! Now we have a database and its table...now, we must inser some informations into the table's fields.
This is an example of query that you can use for insert the infos

INSERT INTO products VALUES (1, 'shirt', 200, 10, 'This is a very nice t-shirt, avaible in all sizes', 'red,darkblue,yellow', 'Tshirt');

Ok, now our database is complete (you can insert a lot of information in the database...in this example we've 5 products in the DB), this is
the "complete" query:

INSERT INTO products VALUES (1, 'shirt', 200, 10, 'This is a very nice t-shirt, avaible in all sizes', 'red,darkblue,yellow', 'Tshirt');
INSERT INTO products VALUES (2, 'shorts', 300, 5, 'A pair of nice shirts for you :)', 'red,darkblue', 'Shorts');
INSERT INTO products VALUES (3, 'bra', 100, 10, 'A nice bra...also if it\'s transparent...', 'white', 'Bra');
INSERT INTO products VALUES (4, 'Shoes', 400, 25, 'A pair of very nice shoes, that you can use in every occasion :)', 'brown, black', 'Shoes');
INSERT INTO products VALUES (5, 'Trainers', 150, 20, 'A pair of trainers, wonderful if you move yourself everyday ;)', 'white,lightblue', 'Trainers'); 

2.2 config file (config.inc.php)

Now, we've our database: we must connect to it and pick the information, if we can have a catalog; well, we'll make this using Php.
We'll have 2 simple php files: config.inc.php:


<?
$server 
"localhost";          // Database Host (usually localhost)
$user "root";          // Username
$pass "";              // Password
$database "catalog";      // Database name
?>

The lines are explained in the script: these are the variables that we'll use for the connection at the database
The file that "pick" the info from the database is "Select.php":

2.3 PHP output file (Select.php)


<?
// Include the external file (the variables for the database connection)
include("config.inc.php");
// Connect at the Db using the &quot;config.inc.php&quot; variables 
$connessione = @mysql_connect($server,$user,$pass);
// Select the database 
$database mysql_select_db($database,$connessione);
// The query text (Select all from the products table in our database)
$select "SELECT * FROM products";
// Make the query
$result mysql_query($select);
// Count the rows (total result)
$rows mysql_num_rows($result);
// For each result 
while($list mysql_fetch_array($result)){
    
// Set some variables, using the info get from the database
    // $list is the array that contains the Db values
    // For example $list[&quot;id&quot;] is the value of  an &quot;id&quot; field in the Db
    
$id $list["id"];
    
$name $list["name"];
    
$stock $list["quantity"];
    
$cost $list["cost"];
    
$description $list["description"];
    
$colors $list["colors"];
    
$image =  $list["image"];
    
// Print in the browser window a string, in a format that Flash can read
    // (Flash read namevar=value&amp;namevar2=valuevar2....)
    // We'll see after why we've some "|"
    
print("Total=$rows&Oggetto$id=$name|$stock|$cost|$description|$colors|$image&");
}
?>