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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c