1

I am pretty new to AS3 and php. Trying to pass an Array from Php into AS3 VO file, then parse it into Vector and then package them in boxes. A very odd thing occurred, the first variable that got pass through is always null, i switched the position around, the first variable is still null. But the rest of the variables are fine. If anyone can fix my problem, would help greatly thanks! Ignore any possibility of SQLi injection problem, i havent have time to change those yet. Thanks!

php

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);

session_start();

include 'connect.php';


$_SESSION['username'];
$username=$_SESSION['username'];


$result=mysqli_query($con,"SELECT * FROM Test WHERE username = '$username'")or die( mysqli_error($con));
$solutions = array();
$check_num_rows=mysqli_num_rows($result);

while ($row = mysqli_fetch_assoc($result))
{

        $solutions[5]=$row['LoZip1'];
      $solutions[2]=$row['rangelow1'];
       $solutions[3]=$row['rangehigh1'];
       $solutions[4]=$row['nobed1'];
}

echo "rangelow1=".$solutions[2];
echo "&rangehigh1=". $solutions[3];
echo "&bed1=".$solutions[4];
echo "&LoZip1=".$solutions[5];
?>

BookVO.as

package  com.clark
{   
    import flash.display.*;
    import flash.net.*;
    import flash.events.*;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLVariables;


    public class BookVO 
    {
        public var bed1:String;
        public var LoZip1:String;
        public var rangelow1:String;
        public var rangehigh1:String;
        public var Bend:URLRequest;
        public var variabless:URLVariables;
        public var nLoader:URLLoader;
        public var callMethod:Function;


        public function BookVO(listener:Function = null)  {


            Bend = new URLRequest("http://localhost/Autoresult.php");
            Bend.method = URLRequestMethod.POST;



            variabless = new URLVariables();
            Bend.data = variabless;


            nLoader = new URLLoader();
            nLoader.dataFormat = URLLoaderDataFormat.TEXT;
            nLoader.addEventListener(Event.COMPLETE,Jandler);
            nLoader.load(Bend);
              if (listener != null) {
                callMethod = listener;
            }
        }

             public function Jandler(event:Event) {
            // handler for the PHP script completion and return of status
            var responseVariables:URLVariables = new URLVariables(event.target.data);
            bed1 = responseVariables.bed1 ;
            LoZip1 = responseVariables.LoZip1;
            rangelow1 = responseVariables.rangelow1;
            rangehigh1 = responseVariables.rangehigh1;

            if (callMethod != null) {
                callMethod(this);
                       }        
            }

    }

}

VectorTest

package  com.clark
{
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.display.Sprite;

    public class VectorTest extends MovieClip 
    {
          public var books:Vector.<BookVO>;
        public function VectorTest() 
        {
    books = new Vector.<BookVO>();

            for (var i:int = 0; i <length; i++) 
            {

            var book:BookVO = new BookVO(response);     
                books.push(book);


            }
        }


            private function response(book:BookVO):void
            {
                trace("Name:",book.bed1);
                trace("Zip:", book.LoZip1);
                trace("ranglow:", book.rangelow1);
                trace("rangehigh:", book.rangehigh1);
                 // call finish() if this is the last book.
                 if (books.indexOf(book) == books.length - 1) {
                finish();
            }
            }


            private function finish():void {
            var currentY:int = 270;

            for (var k:int = 0; k < books.length; k++) 
            {
                var Bolder:Listing2 = new Listing2();
                Bolder.x=80;

                var tf:TextField = new TextField();
                var tf1:TextField = new TextField();
                var tf2:TextField = new TextField();
                var tf3:TextField = new TextField();
                tf2.width = 100;
                tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

                tf.width = 100;
                tf.autoSize = TextFieldAutoSize.CENTER;
                tf1.width = 100;
                tf1.autoSize = TextFieldAutoSize.CENTER;
                tf2.autoSize = TextFieldAutoSize.CENTER;
                tf3.autoSize = TextFieldAutoSize.CENTER;
                tf2.width = 100;
                tf1.y= tf.height+5;


                    // Pulling the textfields content out from the current bookVO

                tf.text = books[k].bed1;
                tf1.text = books[k].LoZip1;
                tf2.text = books[k].rangelow1;
                tf3.text = books[k].rangehigh1;
                tf1.x = (Bolder.height-tf.height)*.5
                tf2.x = (Bolder.height-tf.height)*.5
                tf3.x = (Bolder.height-tf.height)*.5


                tf.x = (Bolder.height-tf.height)*.5
                tf.y = (Bolder.height-tf.height)*.5
                Bolder.addChild(tf);
                Bolder.addChild(tf1);
                Bolder.addChild(tf2);
                Bolder.addChild(tf3);

                    // position the object based on the accumulating variable.
                Bolder.y = currentY;

                addChild(Bolder);
                currentY += Bolder.height + 35;
            }



        }

    }

}
5
  • Please check the response string from PHP. Then you can isolate whether the problem is in PHP side or in AS3 side. Commented Nov 7, 2013 at 6:22
  • Do you mean tracing the event.target.data? Cos on the php side there is no problem. I can trace the data without problem, but parsing it through, the first one always become null, if i move the variables around, the first one becomes null the rest of the variables shows the correct result Commented Nov 7, 2013 at 7:32
  • Please post the trace output of event.target.data. URLVariables code looks okay at a glance. Commented Nov 7, 2013 at 7:37
  • Also, in which line you are getting error 2007? Commented Nov 7, 2013 at 7:41
  • The data are just for test, but here it is rangelow1=b&rangehigh1=bb&bed1=a&LoZip1=bb In output window Name: a Zip: bb ranglow: null rangehigh: bb Error at line 53(tf2.text = books[k].nobed1;) when pulling the textfields out from VO Commented Nov 7, 2013 at 7:46

1 Answer 1

1

I have this simple test code with your test data:

var s:String = "rangelow1=b&rangehigh1=bb&bed1=a&LoZip1=bb"; 
var u:URLVariables = new URLVariables(s); 
trace(u.bed1, u.LoZip1, u.rangelow1, u.rangehigh1); 

And none of them is appearing as null. So parsing seems okay, but the following logic is wrong:

if (books.indexOf(book) == books.length - 1) {
    finish()
}

URLLoader loads data asynchronously, so there is no guarantee that all books will be loaded in the same order as you requested. Based on the network connection the last book data might be loaded before the previous one's data. In that case you will get null value in finish. To solve this instead of using indexof use a counter variable to count how many books are loaded and call finish once counter == books.length.

var counter:int = 0;

private function response(book:BookVO):void {
    counter++;

    if (counter == books.length) {
        finish();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

AWSOME!!!! THANKS SO MUCH!!! Love you man!!! it works!!! Took me so long, thanks so much again!!
ooo i just realised it still doesnt work haha, but i found a quicker solve, i echo a variable that i dont need from php, and doesnt call it in AS3, then it all works.
For some very very odd reason, i am thinking it has something to do with the & in php. because only the first variable has no &.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.