A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: PHP/MySQL/Flash problem

  1. #1
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520

    PHP/MySQL/Flash problem

    I am in way over my head with this one. I don't think that it is particulary difficult but just the lack of experience (none) with using php and MySQL has me flapping!

    The flash file I have consists of a grid containing 10000 squares. I need to reference a MySQL database containing the table "takensquares". "takensquares" has 2 fields - "id" and "taken". "taken" will either have a value of "y" or "n". If the value is "y" then a button will be placed on the corresponding square in the flash file. The location of the button within the stage will be determined by the value in the field "id".

    The way it works is if, for example, only "taken" has y as it's value on "id's" 5, 29 and 4358 then flash will place a button on squares 5, 29 and 4358 on the stage. This is done by a simple maths formula which will place the button at x and y cordinates of a flash file based on the value of id. (ie: square 1 would place the button at x=0 y=0, 2 would place it at x=8 y=0, 5 would place the button at x=40 y=0 etc).

    PHP Code:
    SELECT 
    FROM `takensquares
    WHERE `id` >=1
    AND `id` <=10000
    AND `taken` = 'y' 
    Do I put this php script in the flash file?
    When the query is executed will it send the results to the flash file or do I have to get them?
    Would it be better to have the results passed to a text file and have flash read this file? Perhaps this is the only way to do it?

    I hope I haven't overdone the detail. If there is anything that I maybe haven't made clear or explained correctly then please let me know.

    Any help would be much appreciated. Thanks again!

  2. #2
    Official FK nice guy and MOD 3PRIMATES's Avatar
    Join Date
    Apr 2002
    Location
    Portland Oregon
    Posts
    1,645
    Hey there, well heres what I would do:

    ACTIONSCRIPT:
    PHP Code:
    var loadVars = new LoadVars();

    loadvars.load("http://mydomain/myphpscript.php"this"post");
    loadVars.onLoad msg;

    function 
    msg(success)
    {
    for(
    i=1<= Number(loadVars.cnt); i++)
    {
        
    //loop through the returned databse variables
        //id = the database record id
        
    id this['id'+i];
        
    //taken = the db taken value 
        
    taken this['taken'+id];
        
        
    // this is just extra precaution in case there is a missing record
        // IF there is not a sequential record, then the taken varialbe is set to 'y'
        // you could also just remove the clip or set its _visible to false
        
        // if there is no record for the id
        
    if(id == undefined)
        {
        
    // set taken = yes
        
    _root['mc'+i].taken='y';    
        }
        
    //if there is a record but it doesnt have a taken value
        
    else if(taken == undefined || taken == '')
        {
        
    // set taken to 'n' 
        
    _root['mc'+id].taken='n';    
        }
        else
        {
        
    // else set _root.mcNUMBER.taken to the database value
        
    _root['mc'+id].taken=taken;
        }
    }

    And then the PHP code
    PHP Code:
    <?php

      $host 
    "localhost"//hostname is usually localhost by default
      
    $user ""//insert the name of the user here
      
    $pass "";  //insert the password here
      
    $database "";  //insert name of database wherein table was exported
      
    $table "takensquares";  //insert the name of the table


    $db_connect mysql_connect($host,$user$pass) or die (mysql_error());
    $db_select mysql_select_db($database$db_connect) or die (mysql_error());

    $sql "select * from `".$table."` LIMIT 10000";
    $result mysql_query($sql) or die(mysql_error());

    // loop through the array and output the scores
    while($row mysql_fetch_array($result))
    {
    echo 
    "&id".$row['id']."=".$row['id']."&taken".$row['id']."=".$row['taken']."&cnt=".$row_count."&";

    $row_count++;
    }

    ?>
    If you need any help, I just had this running on my system.

    3P
    Last edited by 3PRIMATES; 11-16-2007 at 11:31 PM.

  3. #3
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    Wow - what an answer! Much more than what I was expecting!

    I assume it would be faster to have only an id field. ie: rather than sequential numbering from 1 to 10000 with values in each one; that it would be better to just have flash place squares where there is a value in id.

    ie: buttons would be placed on squares for the values of id meaning that the "taken" field wouldn't need to exist. I do not want flash or php adding values if there is none there.

    I need to disect the code and figure it all out. Thanks!

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