Welcome, Guest. Please login or register.
Did you miss your activation email?
05/21/12, 03:09
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  The Library
| |-+  Technical Reference Area (Moderators: Flash-db, Musicman, BurtonRider1983, vesa kortelainen, Ronald Wernecke, Jorge Solis)
| | |-+  Populate MX Combo Box/ Drop down from DB/Text file
0 Members and 2 Guests are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: Populate MX Combo Box/ Drop down from DB/Text file  (Read 12963 times)
Flash-db
Administrator
Systems Administrator
*****
Posts: 1867



View Profile WWW
« on: 06/21/02, 18:10 »

Dynamically Populate the MX Combo Box Component

This reference goes over how to populate the Flash MX combo Box component from either

1: A text file,
2: An Array in PHP or ASP,
3: A Database (MySQL) with the help of PHP
4: XML.

Edit:  This was written a while ago, please refer to http://www.flash-db.com/Components/ for quite a few examples of the XML version.

This allows you to make more complex forms and easily change around what is displayed from an external datasource. The text file example is not as powerful because you will still have to change around the contents of that once in a while.  To have a truly dynamic form you'll want to use a database to store what is displayed in your drop down / combo box form.

First where going to go over the Flash side of things.  The Flash side basically consists of dragging a Combo Box component onto the stage then creating 2 functions.

So let's follow somewhat of an order:

1) Drag a Combo Box component onto the stage.  Then open up the 'Properties' window for the combo Box.  Give it an instance name of dropDown.

2) Create two text fields named 'NameBox' and 'DataBox' - these are used only to display what we select from the drop down menu.

3) Add the following 2 Functions to a Frame Action (call the frame functions), these two functions allow us to read in the data as well as set the onChange handle so the drop down box does something when a user selects an Item.  In this case where only going to be displaying the Name and Data from the combo box in separate text fields.

Code:
function AddItems() {
                // Reads in info and adds them to the combo box.
   for (i=0; i<NumItems; i++) {
      var Name    = eval("Name"+i);
      var DataRow    = eval("DataRow"+i);
         dropDown.addItem(Name, DataRow);
   }
   //Set ChangeHandler
   dropDown.setChangeHandler("SelectItem");
}

function SelectItem(){
  // This sets the values for the label and data parts of the
  // combo box to the two text fields named 'NameBox' 'and DataBox'
  // This is the function called / set by the Change handler of the Combo Box.

   NameBox   = dropDown.getSelectedItem().label;
   DataBox   = dropDown.getSelectedItem().data;
}

Ok.  Now we just need to Load in our text file.  Add the following Actionscript to a different frame.

Code:
_root.Go = "";
_root.Thinking.gotoAndPlay(2);
loadVariables("LoadData.php", this);

The variable 'Go' tells us that the data has been loaded.  We set this initially to zero so that we can test for it later.  Then we tell the Thinking Movie clip to gotoAndPlay frame number 2.  The job of the 'Thinking' Movie Clip is to tell us when are data has been loaded - and what to do with it.   Finally we use loadVariables to load in our Combo Box selections.  The 'LoadData.php' part will be whatever the name of the file your loading is.  If it's a text file for example it will be 'SomeData.txt'.

One important part to go over is the Thinking Movie Clip.  (If you open up the download this is easier to understand).  The Thinking Movie clip is basically just a loop that waits for data to be returned, as soon as the data is returned - It calls a Function.  Here is the basic code used in the Thinking Movie Clip.

Code:
if (_root.Go == "Yes") {
   _root.AddItems();
   this.gotoAndStop(1);
}

The 'Go' value is sent back from the database (as shown later).  If the value of 'Go' is yes - then we invoke the function AddItems();  This function is on the _root level of the stage so we need to specify that.  We also tell the thinking movie Clip to stop looping with the last line (this.gotoAndStop(1));

Ok that's it for the Flash side

Now onto the the text file and / or database part.

Ok - the main thing here is how the data is formatted.  

The main advantage PHP has over using a text file is that it automatically creates the correct format to use.  The main advantage using a database has over using a text file or just a PHP array - is that it's much easier to change and can change dynamically at any time.

Here's the basic format that where going to work with:
&Name0=SomeComboName&DataRow0=DataRowData&Name1=SomeComboName&DataRow1=DataRowData&Go=Yes

As you can see it would be fairly tedious to Actually write this out by hand.  But that is the only text you would need in your text file for this to work.  Obviously you would want to change around some of the values.  Name0 would be the first Item in your Combo Box - which would have a value of SomeComboName.  DataRow0 would be the value of Name0 - which in this example would be 'DataRowData'

So if you wanted to hook that up to the Combo box - just create a text file with that example line as it's contents - and then change the loadVariables function in Flash to load that text file.  

A little PHP makes things easier
Ok - so we don't want to write out all of that text in that format for each different Combo Box we have.  This PHP script will take care of that for you.  It basically formats out that string automatically from an Array of values instead.  Here's the script:

Code:
<?php
// Basically comma seperated array's..
$NameArr = array("Jeff""Jim""Bob");
$DataArr = array("Some info about Jeff""Some info About Jim""Some info About Bob");

// counts the number of in each array.
$numReturn count($NameArr);

$i 0;
print 
"&";
                
// This while loop just loops through each array element and puts them in the necessary format.  
   while ($i $numReturn) {
      $Name       $NameArr[$i];
      $DataRow    $DataArr[$i];

                                
// Prints out the line that is sent back to flash.
      print "Name$i=$Name&DataRow$i=$DataRow&";
      $i++;
   }
// sends the number of items in the drop down box - plus tells the thinking Movie clip that the script is complete.
print "&NumItems=$numReturn&Go=Yes&";

?>


- Ok so overall fairly easy.  This is one step closer to a dynamic combo box.  The final step involves using a database to store the items your going to display in the combo box.  First lets take a look at the script that would do this:
Code:
<?php
// Connets to your database.
require('Connect.php');
$table "ComboData";

$SelectRows mysql_query("SELECT * FROM $table"$Connect);
$numReturn mysql_num_rows($SelectRows);

$i 0;
print 
"&";

   while ($i $numReturn) {
      $Name       mysql_result($SelectRows,$i,"Name");
      $DataRow    mysql_result($SelectRows,$i,"DataField");

      print "Name$i=$Name&DataRow$i=$DataRow&";
      $i++;
   }
print 
"&NumItems=$numReturn&Go=Yes&";

?>


This is basically the same as above - except it is reading data from a MySQL database instead of just a text file or Array.

In this case you would need a Database with the following Columns:
ID, Name and DataField.  The SQL for creating a table like this would look like:

CREATE TABLE comboData (
  ID int(11) NOT NULL auto_increment,
  Name tinytext NOT NULL,
  DataField tinytext NOT NULL,
  PRIMARY KEY  (ID)
) TYPE=MyISAM;

If your using PHPMyAdmin - you can just load this right in and it will take care of everything for you.  You will need to add the correct DBName, DBUser, and DBPass info to an include script - in this case we called it 'Connect.php' which we included at the top of this script.

And that's about it.  If you have any questions ask away below.

Also you may want to take a look at the Poll Application and go over that Fla/Code - it uses basically the same method:  http://www.flash-db.com/Poll/



« Last Edit: 06/26/02, 16:41 by Flash-db » Logged

-Jeff.
Gary
Server what's that
*
Posts: 5



View Profile Email
« Reply #1 on: 12/17/02, 21:14 »

http://URL

Great tutorial!

I can get very thing to work except go to URL. I noticed that you have left off the

getURL(URL, "_blank");
}

On the  < function SelectItem() >  function that was attached to the original combo box example.

Any suggestions would be appreciated.

Thank you for your time.  
Logged
Glen Baird
Server what's that
*
Posts: 1



View Profile Email
« Reply #2 on: 07/01/03, 08:43 »

Works great!

Except when I try to load it as a movie into a movie clip. Using -- loadMovie("ComboBox_PHP_Link.swf", _level0.myMovieclip);

However, It works if I load it into it's own level -- loadMovieNum("ComboBox_PHP_Link.swf", _level1);

I need to load it into a movie clip to control it's positioning and size.

I have fiddled for about 6 hours so far and no luck. Any suggestions?



Logged
Naomi White
Server what's that
*
Posts: 6



View Profile Email
« Reply #3 on: 08/12/04, 09:22 »

 Huh
It doesn't work with Flash MX2004 pro! I don't understand: I get data (the names of my friends) from Access db into the combobox using ASP, but I can't select any name! Whatever I select, the combobox stucks on the first name. Any suggestion why?

with my regards,
Naomi
Logged
pentapus
Server what's that
*
Posts: 1

better eight than five


View Profile Email
« Reply #4 on: 03/15/05, 09:28 »

I'm just a beginner in MySQL.

I think this tutorial very appropriate to learn how to replace data coming from a text file for others that would come from a db.
I intend to make an Address Book similar of that of the example that I have seen in the section Components (ListBox-An Address Book using Flash MX components, XML and PHP. ) but without using XML.

I have read "(If you open up the download this is easier to understand)" but I don't find anything to download, maybe I don't understand to that refers. Can you suggest me some .fla source file that doesn't use XML (just PHP+MySQL).

Thank you so much.

Logged
vesa kortelainen
Administrator
Systems Administrator
*****
Posts: 3450


View Profile
« Reply #5 on: 03/15/05, 14:51 »

Questions, Feedback, Praise or whine:

Post your messages to Flash MX Components section. This section is ment to be for tutorials/articles only.

Thank you!
Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!