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

时间:2013-12-03 13:50:30

标签: php arrays actionscript-3 actionscript

我是php和AS3的新手,尝试搜索php并将循环数组解析为AS3。但不太确定如何,因为我将每个部分与&amp ;.分开。但是当它被环回时数组的结尾,没有&附加,所以整个数组是第一项合并到最后。并且第一项返回null。

我尝试将event.target.data跟踪到动态文本字段中,第一个项目返回null,并合并到最后一个项目中。

的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'];

        }

}


?>

因为“nobed =”没有&amp;,所以最后一项PRICE不会以&amp; ,因此循环无法正确分离和显示。而且当我试图添加一个&amp;,在nobed之前它也显示错误。它在webbrowswer中没有任何问题。

示例结果(粗体部分是发生循环问题的地方)

nobed = 3及压缩= 19104&安培;位置= TestListing&安培;价格= 750nobed = testing3&安培;拉链= testing3&安培;位置= testing3&安培;价格= testing3

当我尝试回复第一部分时,在动态文本中对其进行回复,并说Error #2007: Parameter text must be non-null.因为我不能放一个&amp;在nobed之前,结果nobed合并到Location,所以nobed变为Null。

当我尝试将event.target.data设置为带有&amp;的动态文本字段时在面前被称为“&amp; nobed =”然后我有Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

任何想法我应该如何解决它?谢谢你的时间。希望我的问题不是太新手。

AS3代码

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




}

当我点击AS3中的搜索按钮并启动事件ASandler时,它会立即喊出错误,输出窗口中没有任何内容。

1 个答案:

答案 0 :(得分:1)

也许最好使用数组来存储结果并将数组编码为json并在as3中解析它。

例如:

$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 );

在您的AS3代码中,您可以从json字符串中获取对象,如下所示:

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

在这种情况下,您的resultObject应该是一个包含所有行的数组。

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

[编辑]
如果要为每个阵列部件命名,可以执行以下操作:

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

} 

尝试使用这样的简单php脚本:

<?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) );
?>

我认为您有服务器配置问题