dongmi8980 2013-11-07 05:08
浏览 39
已采纳

Php Array进入AS3第一个变量null错误,其他变量都很好。 有什么想法吗? 错误#2007

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



        }

    }

}
  • 写回答

1条回答 默认 最新

  • douxia2053 2013-11-07 08:42
    关注

    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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?