Php Array into AS3 first variable null error , other variables are all fine. Any thoughts? Error #2007

StackOverflow https://stackoverflow.com/questions/19828406

Вопрос

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;
            }



        }

    }

}
Это было полезно?

Решение

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();
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top