0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

19 Comments

Thanks, but just wondering does that mean, i can't assign a name to each array part? like "nobed=" "&zip=" etc? Cos i will have to parse that separate the Array inside AS3 and place them accordingly
the problem is if you have more than one nobed or zip or other value how can you get them, you will get just one. Or you want to group them like nobed=result1,result2,result3... ? In all case my preference is for json encoded object than URLLvariables, and you can have the result nobed=result1,result2,result3 easyly too
ok sweet, i just got home from work. I will give that a try in a little. And yer i will have a few results from each one, so i assume i will have to return it like nobed=result 1,result2,result3... Because before they were in 1 long chain, nobed=result1&location=result1 and looped etc
see my edited answer i show you how to prepare your results in php and get them back in flash
I am getting this error Error #1132: Invalid JSON parse input., but i will have a try again after dinner
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.