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.