A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] ask About Combo box

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    Nanggroe Aceh Darusalam
    Posts
    84

    resolved [RESOLVED] ask About Combo box

    how to make the combo box take the data from table in a database ?

    i'm using phpmyadmin as the database server

    please help and if you dont mind give me some example

    thnks in adv
    maybe I'm old, but I want to learn more

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864

    Tutorial [part 1/2]

    First of all, you have to make a PHP (or some other server-side scripting language) file and make it fetch data from your database. Then, you call this PHP file through Flash to make it execute its codes and fetch data from your database, and make Flash receive certain variables passed from the PHP file, containing the database table information. This passed data can then be manipulated at will, like for instance distributed to ComboBox items.

    ------------------

    - TUTORIAL: DATABASE INFORMATION TO FLASH [AS2] -

    INTORDUCTION
    In this tutorial, I am going to utilize PHP's built-in mySQL manipulation, to pull out data from mySQL database tables and pass them into Flash, and then insert this information into a ComboBox. The database related images are from phpMyAdmin.

    REQUIREMENTS: A server, and PHP installed in it

    STEP 1 - CREATING THE PHP FILE
    Open your favorite Text Editor (e.g. Notepad, Notepad++, Dreamweaver), and start by typing this:

    PHP Code:
    <?php

    $database_host 
    "localhost";
    $database_user "some_username";
    $database_pass "some_password";
    $database_name "chemroom_1";
    $database_table "naushad_organisation";
    The first line indicates, "here starts PHP code", because you can combine markup languages with PHP (like HTML), hence it's important to state when we're starting to write PHP codes.

    The rest of the 5 lines are variables which store:

    Database Host - this is usually localhost
    Database Username - your database username
    Database Password - your database password
    Database Name - the name of the database in mysql you wish to use
    Database Table - the table within the database you've chosen



    This information is used to connect to the database, select the mysql database and then pull out information from the specified table. Speaking of connecting, that's what we're gonna do next, so in the same file, type this:

    PHP Code:
    $database_connection mysql_connect($database_host$database_user$database_pass); 
    We use this to connect to the database, or at least check whether or not the connection was successful, and that is done by simply checking what $database_connection variable returned (if it returned FALSE, then the connection failed, otherwise it was successful):

    PHP Code:
    if(!$database_connection){
        
    // connection failed
    } else {
        
    // connection successful 
    In the successful connection part, add this:

    PHP Code:
    mysql_select_db($database_name);
    $query "SELECT * FROM $database_table";
    $data mysql_query($query);
    $rows_length mysql_num_rows($data); 


    With the first line, we select the database we want to select, in this case, it's chemroom_1. Second line is a variable which stores a mySQL query in SQL language, which is basically the language to communicate with databases. In this case, we SELECT *, which means everything inside our selected database, FROM $database_table, the table we assigned at the start of the coding. Simply put, we select all data from our desired table (in this case, naushad_organisation). The third line executes the mySQL query in line 2 (which was just a string), and the fourth line stores the number of rows in our table.

    Next, add this:

    PHP Code:
    if($data){
            
        
    $db_labels = array();
        
    $db_data = array(); 
    We check if the mySQL query was successfully executed. Assuming it was, we move into the successful part, and create 2 array variables, one to store the labels fetched from our table, and the other to store the data fetched from the same table. This is because ComboBox has a label which shows up when you open it, but the corresponding data can be different. For instance, if your label says, "One", then the corresponding data might be, 1, and as I don't know whether you want the label and data to be the same or not, I've assumed that you want them to be different and their values to be fetched from your database.

    Continuing, type this:

    PHP Code:
    while($rows mysql_fetch_array($data)){
                
        
    array_push($db_labels$rows[1]);
        
    array_push($db_data$rows[0]);
                

    Using a while loop, we run it as long as $rows variable is holding mysql_fetch_array($data), which fetches a row at a time from your table and stores all the columns in an array. What the while loop does, is that it runs through all the rows, and once it reaches the end, it stops. So we push all values from from Column 1 ($rows[1]) to $db_labels array, because that's where my labels are, and all the values from Column 2 ($rows[0]) to $db_data array.



    Now to send the variables to Flash. Type this after the above:

    PHP Code:
    echo "&labels=";
            
    for(
    $i=0;$i<count($db_labels);$i++){
        if(
    $i == count($db_labels)-1){
            echo 
    $db_labels[$i];
        } else {
            echo 
    $db_labels[$i] . ",";
        }

    First of all, we type out, &labels=, because we are going to make Flash read all the variables in this PHP file, and PHP codes are not visible because they're executed on the server before the page is loaded, so we need to write out the variables we want to send by using echo (which is a function similar to trace(), but it actually types something on the document). So we create a variable named, labels (the & is used to seperate variables from each other, since we're going to add another one for the data), and then run a for loop (as long as the length of $db_labels array), and type out all the values found with a comma seperator, so that it looks, e.g., something like this: value1,value2,value3 - the if statement is used to check for the last value in the loop, so that we can omit the comma for the last value, otherwise it won't work as planned (at least that's what I experienced when trying it). This is NOT an array, it's just a string (but we're going to turn it into an array once it's imported into Flash).

    We need to do the same with the data fetched from our database, so type this after the previous code:

    PHP Code:
    echo "&data=";
            
    for(
    $i=0;$i<count($db_data);$i++){
        if(
    $i == count($db_data)-1){
            echo 
    $db_data[$i];
        } else {
            echo 
    $db_data[$i] . ",";
        }
    }

    echo 
    "&"
    This should be pretty self-explanatory after explaining the previous code, but you've probably noticed the extra & written out at the end. This shouldn't be a problem to omit, but just to be on the safe side, we include this to end the variable.

    And now, just close the if statements and end the PHP code:

    PHP Code:
        }
        
    }

    ?> 


    Now we're done with the PHP file. Here's the full code:

    PHP Code:
    <?php

    $database_host 
    "localhost";
    $database_user "some_username";
    $database_pass "some_password";
    $database_name "chemroom_1";
    $database_table "naushad_organisation";

    $database_connection mysql_connect($database_host$database_user$database_pass);

    if(!
    $database_connection){
        
    // connection failed
    } else {
        
        
    mysql_select_db($database_name);
        
    $query "SELECT * FROM $database_table";
        
    $data mysql_query($query);
        
    $rows_length mysql_num_rows($data);
        
        if(
    $data){
            
            
    $db_labels = array();
            
    $db_data = array();
            
            while(
    $rows mysql_fetch_array($data)){
                
                
    array_push($db_labels$rows[1]);
                
    array_push($db_data$rows[0]);
                
            }
            
            
    /* SEND LABELS */
            
            
    echo "&labels=";
            
            for(
    $i=0;$i<count($db_labels);$i++){
                if(
    $i == count($db_labels)-1){
                    echo 
    $db_labels[$i];
                } else {
                    echo 
    $db_labels[$i] . ",";
                }
            }
            
            
    /* SEND DATA */
            
            
    echo "&data=";
            
            for(
    $i=0;$i<count($db_data);$i++){
                if(
    $i == count($db_data)-1){
                    echo 
    $db_data[$i];
                } else {
                    echo 
    $db_data[$i] . ",";
                }
            }
            
            echo 
    "&";
            
        }
        
    }

    ?>
    Save the file as whatever you want, but the file extension needs to be .php - in this tutorial, I named mine, test.php. Go ahead and upload it to your server!
    Last edited by Nig 13; 05-22-2012 at 05:04 AM.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864

    Tutorial [part 2/2]

    STEP 2 - IMPORT VARIABLES FROM PHP FILE
    The PHP file won't automatically send the variables to Flash, it only typed them out. To import them, we need to call the PHP file from Flash, to read the variables and fetch their values, so that we may use them inside Flash.

    Create a new Document. Select Frame 1, and open the Actions Panel [F9], and type this:

    Actionscript Code:
    loadVars = new LoadVars();
    loadVars.onLoad = function(success){
        if(success){
            labelArray = this.labels.split(",");
            dataArray = this.data.split(",");
            sendToComboBox();
        }
    }

    sendVars = new LoadVars();
    sendVars.sendAndLoad("http://www.yourwebsite.com/test.php", loadVars, "POST");

    First of all, we're creating a new LoadVars (object used for example to read variables from a file), which when loaded (onLoad) will create 2 Arrays, labelArray and dataArray. labelArray will store the imported labels array variable from the PHP file, and seperate the values with the seperator, "," (comma), which will then turn it into an array. For example, say the imported String from PHP is, value1,value2,value3 - but since it's a string and we want convert it to an Array, we use Array.split() to split the values with the specified seperator (in this case, a comma), turning it into an array with the first value as, value1, second value, value2, and third value, value3. sendToComboBox() means we're calling a function with the same name, but it's not important in this step, so leave it for now (explained in Step 3).

    sendVars is another LoadVars object, but we're only using this one to fetch the variables from the PHP file, using sendAndLoad function, which sends variables from Flash to the target file, and loads variables from the target file into Flash, which is registered by another LoadVars object (in this case, loadVars). However, we're not sending any variables, but only using this to actually call the PHP file, because when we do that, it'll execute its codes, typing out the fetched data from your database to the PHP file, and then this data is imported into Flash for loadVars (the other LoadVars object) to register. The imported variables are then embedded into loadVars object (hence we use the this scope when referring to data and labels when turning them into arrays in Flash).

    Now the variables are successfully imported into Flash.

    STEP 3 - DISTRIBUTING VARIABLES TO COMBOBOX
    Now we've finally come to the step where we add the passed variables into a ComboBox.

    Click on Window->Components [CTRL+F7], expand User Interface, and drag and drop a ComboBox to the Stage. Select the ComboBox, open Properties Panel [CTRL+F3], and give it an Instance Name - I'm going to use, comboBox, for this tutorial. Select Frame 1 again, open the Actions Panel, and at the bottom (after the previously written code), type this:

    Actionscript Code:
    function sendToComboBox(){
        for(i=0;i<labelArray.length;i++){
            comboBox.addItem({label:labelArray[i], data:dataArray[i]});
        }
    }

    You remember we called sendToComboBox function in the onLoad handler for loadVars object? Well, here we're actually creating that function, so that it's called after the passed variables are turned to Arrays. In this function, we run a for loop as long as one of the Array's lengths (doesn't matter as both of their lengths are the same). In this for loop, we're adding items to our comboBox, where the labels are elements from our labelArray, and the corresponding data are elements from our dataArray. With this, we're done!

    CONCLUSION
    For this to work, you'll need to have a server and PHP installed in it (as already mentioned), and when testing it, the PHP file needs to be uploaded to your server, but the Flash file isn't necessary to upload as well, because LoadVars can be used locally (if you try it locally, remember to specify full path to PHP file, including your website address).

    -----------------

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  4. #4
    Member
    Join Date
    Feb 2012
    Location
    Nanggroe Aceh Darusalam
    Posts
    84
    thanks nig

    its helpfull
    maybe I'm old, but I want to learn more

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center