duanci6484 2013-12-03 13:50
浏览 36
已采纳

如何分离AS3的php Array Loop的结束和启动

I am new to php and AS3, trying to do a search php and parse the looped array into AS3. But not very sure how, as i separate each parts with &. But the end of the array when it is looped back, there is no & attached, so the whole array is first item is merged into the last. And the first item is returned null.

I tried tracing the event.target.data into a dynamic text field, the first item return null, and merged into the last one.

Search.php

<?php

ini_set('display_errors', 1); error_reporting(E_ALL);


session_start();

include 'connect.php';

if($_POST) 
{
$nobed = ($_POST['nobed']);
$Location = ($_POST['Location']);
 $zip = ($_POST['zip']);
 $price = ($_POST['price']);

 $sql = array();

if (!empty($nobed)) {
    $sql[] = "nobed='$nobed'";

}
if (!empty($Location)) {
    $sql[] = "Location='$Location'";
}

if (!empty($zip)) {
    $sql[] = "zip='$zip'";
}
if (!empty($price)) {
    $sql[] = "price='$price'";
}

$sql = implode(' AND ', $sql);
$sql = "SELECT * FROM Listing" . (!empty($sql)? " WHERE " . $sql: '');

$result = mysqli_query($con,$sql);

        $solutions = array();


        while ($row = mysqli_fetch_assoc($result))


        {

     echo "nobed=".$solutions[1]=$row['nobed'],"&zip=".$solutions[2]=$row['zip'],"&Location=".$solutions[3]=$row['Location'],"&price=".$solutions[4]=$row['price'];

        }

}


?>

Because the "nobed=" has no &, so the last item PRICE does not end with a& , so the loop can not be separated and displayed correctly. And also when i tried to add a &,before nobed it displays error as well. It echo with no problem in a webbrowswer.

Example result(bold part is where the loop issue occur)

nobed=3&zip=19104&Location=TestListing&price=750nobed=testing3&zip=testing3&Location=testing3&price=testing3

When i try to echo the first part nobed and trace that in a dynamic text, it says Error #2007: Parameter text must be non-null. Because I can not put a& before nobed, the results nobed merged into Location, so nobed becomes Null.

When i try to set event.target.data into a dynamic text field with a& infront of nobed as"&nobed=" then i have Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

Any Idea how i should approach it to fix it? Thanks for your time. Hope my question isn't too newbie.

AS3 code

       function Asandler(event:Event):void{

var resultString  :String = event.target.data;
// parse result string as json object
var resultObject  :Object  = JSON.parse(  resultString );
// loop all keys in the object
for( var s:String in resultObject )
{
    // trace key => value
    trace( nobed, resultObject[s] );  
    trace( Location, resultObject[s] );
}

           } 

Php

$nobed1 = array();
    $zip1= array();
    $Location1 = array();
    $price1 = array ();
    // create all you want


       while( $row = mysqli_fetch_assoc($result) !== false )
{
    // add result row to your output's next index
    $nobed1[] = $row['nobed'];
            $zip1[] = $row['zip'];
            $Location1 = $row ['Location'];
            $price1 = $row ['price'];
            //...
}

    // echo the json encoded object
echo json_encode( array('nobed'=>$nobed1, 'zip'=>$zip1,'Location'=>$Location1,'price'=>$price1 ) );




}

When i hit the search button in AS3, and fire up the event ASandler, it shouts the error straight away, with nothing in the output window.

  • 写回答

1条回答 默认 最新

  • doubo1871 2013-12-03 14:21
    关注

    Maybe it would be better to use an array to stock your results and encode your array to json and parse it in as3.

    Ex:

    $result = mysqli_query($con,$sql);
    
    // create your output array
    $output = array();
    
    // fetch your results
    while( $row = mysqli_fetch_assoc($result) !== false )
    {
        // add result row to your output's next index
        $output[] = $row;
    }
    
        // echo the json encoded object
    echo json_encode( $output );
    

    In your AS3 code you can get the object from the json string like this:

    // retrieve data from php call
    var resultString:String = yourLoader.data;
    // parse result string as json object
    var resultObject:Object = JSON.parse( resultString );
    

    In this case your resultObject should be an array containing all your rows.

    // retrieve data from php call
    var resultString :String = yourLoader.data;
    // parse result string as json object and cast it to array
    var resultArray  :Array  = JSON.parse( resultString ) as Array;
    // get the length of the result set
    var len:int = resultArray.length;
    // loop the result array
    for( var i:int = 0; i<len; ++i )
    {
        // trace nobed value
        trace( resultArray[i].nobed );    
    }
    

    [EDIT]
    If you want to name each array part you can do this:

    PHP:

    $result = mysqli_query($con,$sql);
    
    // create your outputs array
    $nobed = array();
    $zip = array();
    $Location1 = array();
    $price1 = array();
    // create all you want
    
    // fetch your results
    while( $row = mysqli_fetch_assoc($result) !== false )
    {
        // add result row to your output's next index
        $nobed[] = $row['nobed'];
        $zip[] = $row['zip'];
        $Location1[] = $row ['Location']; // you forgot the [] here meaning add to the end of the array
        $price1[] = $row ['price']; // you forgot the [] here meaning add to the end of the array
        //...
    }
    
    // echo the json encoded object
    echo json_encode( array('nobed'=>$nobed, 'zip'=>$zip, 'Location'=>$Location1, 'price'=>$price1) );
    

    AS3:

    function Asandler(event:Event):void
    {
        // trace your recived data so you can see it before any parsing error
        trace( event.target.data );
    
        var resultString  :String = event.target.data;
        // parse result string as json object
        var resultObject  :Object  = JSON.parse(  resultString );
        // loop all keys in the object
        for( var s:String in resultObject )
        {
            // you cannot do this as nobed and Location object don't exists i think, you can trace string or properties maybe trace( 'nobed', resultObject[s] ) but as s is not nobed all the time it's not correct
            //trace( nobed, resultObject[s] );  
            //trace( Location, resultObject[s] );
    
            // so maybe add a switch case to make specific operation in function of the key
            // with switch case you can make a specific code block for a specific value of the variable passed in the switch
            switch(s)
            {
                // if( s == 'nobed' )
                case 'nobed'
                    trace( 'nobed', resultObject[s] );
                    // do what you want with your nobed array
                    break;
                // if( s == 'zip' )         
                case 'zip'
                    trace( 'zip', resultObject[s] );
                    // do what you want with your zip array
                    break;
                // if( s == 'Location' )            
                case 'Location'
                    trace( 'Location', resultObject[s] );
                    // do what you want with your Location array
                    break;
                // if( s == 'price' )
                case 'price'
                    trace( 'price', resultObject[s] );
                    // do what you want with your price array
                    break;
            }
        }
    
    } 
    

    try with a simple php script like this:

    <?php
    $row = array();
    for( $i=0; $i<10; $i++ )
    {
        $row[] = array('nobed'=>'nobed'.$i, 'zip'=>$i, 'Location'=>$i, 'price'=>$i);
    }
    
    // create your output array
    $nobed1     = array();
    $zip1       = array();
    $Location1  = array();
    $price1     = array();
    
    // fetch your results
    for( $i=0; $i<10; $i++ )
    {
        // add result to your output next index
        $nobed[]        = $row[$i]['nobed'];
        $zip[]          = $row[$i]['zip'];
        $Location1[]    = $row ['Location']; // you forgot the [] here meaning add to the end of the array
        $price1[]       = $row ['price']; // you forgot the [] here meaning add to the end of the array
    }
    
    echo json_encode( array('nobed'=>$nobed, 'zip'=>$zip,'Location'=>$Location1,'price'=>$price1) );
    ?>
    

    I think you have a server configuration problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么