Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sending a matrix from PHP to AS3...

    Hello!
    I have a bit of a problem. I need to send a two dimensional array from PHP to AS3 and save the data in AS3 in a two dimensional array, but I can't find a way. Here is my code:

    adatatvitel.php
    PHP Code:
    <?php

    require("db.php");

    $sql "SELECT * FROM kerdesvalasz";
    $result mysql_query($sql) or die(mysql_error());

    $adatok = array
     (
      
    "kerdes" => array
      (
      ),
      
    "valasz1" => array
      (
      ),
      
    "valasz2" => array
      (
      ),
      
    "valasz3" => array
      (
      ),
      
    "valasz4" => array
      (
      ),
      
    "helyes" => array
      (
      )
     );
     
    $i=0;
    while(
    $row mysql_fetch_array($result))
    {
      
    $adatok['kerdes'][$i] = $row ['kerdes'];
      
    $adatok['valasz1'][$i] = $row ['valasz1'];
      
    $adatok['valasz2'][$i] = $row ['valasz2'];
      
    $adatok['valasz3'][$i] = $row ['valasz3'];
      
    $adatok['valasz4'][$i] = $row ['valasz4'];
      
    $adatok['helyes'][$i] = $row ['helyes'];
      
    $i++;  
    }

    for(
    $i=0;$i<17;$i++)
    {
      echo 
    "adatok=".$adatok['kerdes'][$i];
      
    $temp $_POST['adatok'];
      if(
    $temp == $adatok['kerdes'][$i])
      {
      
    ob_clean();  //clrscr();
      
    }
    }

    ?>
    I just want to send the adatok[kerdes][$i] elements of the array first.

    Code:
    stop();
    
    var loader:URLLoader=new URLLoader();
    loader.dataFormat=URLLoaderDataFormat.VARIABLES;
    loader.addEventListener(Event.COMPLETE,onComplete)
    
    function sendIt(e:MouseEvent):void
    {
    var request:URLRequest=new URLRequest("login.php");
    request.method=URLRequestMethod.POST;
    var vars:URLVariables=new URLVariables();
    vars.user=user_txt.text;
    request.data=vars;
    loader.load(request);
    }
    
    function onComplete(e:Event):void
    {
    var request:URLRequest=new URLRequest("login.php");
    request.method=URLRequestMethod.POST;
    var loader:URLLoader=URLLoader(e.target);
    var vars:URLVariables=new URLVariables(loader.data);
    gotoAndStop(2);
    uzenet.text=vars.user;
    }
    
    login_btn.addEventListener(MouseEvent.CLICK,sendIt);
    start_btn.addEventListener(MouseEvent.CLICK,startquiz);
    
    var adatok:Array = [];
    var valtozo:String = new String();
    var i:int = 0;
    var j:int = 0;
    var szamlalo:int = 0;
    
    for(i=0;i<17;i++)
    {
    	adatok[i] = [];
    }
    i=j=0;
    
    function startquiz(e:MouseEvent):void
    {
    var request:URLRequest=new URLRequest("adatatvitel.php");
    request.method=URLRequestMethod.POST;
    var loader:URLLoader=URLLoader(e.target);
    while(szamlalo<17)
    {
    var vars:URLVariables=new URLVariables(loader.data);
    adatok[i][j] = vars.adatok;
    request.data = vars.adatok;
    j++;
    szamlalo++;
    }
    gotoAndStop(3);
    }

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hi,
    When transfering data from php to another aplication, it is indicated to use JSON format.
    In your case, you can store into an array the data you want to send to AS3, then use json_encode() and output the json string.
    Code:
    $arrtosend = array();
    while(...) {
      ...
      $arrtosend[] = $row ['valasz1'];
    }
    
    echo json_encode($arrtosend);
    In AS3 you can convert the json string back into an object /array, but i don't know the function for AS3.


  •  

    Posting Permissions

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