I've got this code that retrieve data from my SQL Data Base into my AS3 code.
My table (data base) has this rows : "id", "title", "price", "information", "mail".
In my AS3 code I loaded "title".
Php Code :
header('Content-type: application/json');
$ts = gmdate("D, d M Y H:i:s") . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
$num = mysql_numrows($sql_result);
$phptheTitle = "";
$obj = array();
while ($row = mysql_fetch_array($sql_result)) {
$theTitle = $row["theTitle"];
$phptheTitle = $phptheTitle.' </br> '.$theTitle;
$datas = array();
$datas['theTitle'] = $row["theTitle"];
$datas['prix'] = $row["prix"];
$obj[] = $datas;
}
echo json_encode( array('products' => $obj) );
mysql_free_result($sql_result);
mysql_close($connection);
?>
AS3 code :
function categorieSelected(evt:Event):void {
var urlReq:URLRequest = new URLRequest ("http://www.mywebsite.com/find_annonces.php");
urlReq.method = URLRequestMethod.POST;
var urlVars:URLVariables = new URLVariables();
urlReq.data = urlVars;
trace("typeSelected");
urlVars.categorie = evt.target.value;
trace(urlVars.categorie);
varLoader2.load(urlReq);
var loader:URLLoader = new URLLoader (urlReq);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
trace(urlReq);
loader.addEventListener(Event.COMPLETE, loadComplete);
}
function loadComplete(evt:Event):void {
trace("loadComplete");
var myResult:String = evt.target.data;
trace(myResult);
output_txt.htmlText = myResult;
var datas :Object = JSON.parse( myResult );
var products :Array = datas && datas.products ? datas.products : [];
var len:int = products.length;
for( var i:int = 0; i<len; ++i ){
trace( products[i].title, products[i].price );
}
}
So my output_txt is displaying all items in the row "title".
Now, is it possible to create a link for each title(in AS3) ? In order to display "price", "information", "mail" when we click on the title(each title has their own "price", "information" and "mail", contain in my database).
Exemple : The AS3 code displays "Computer". When I click on "ipod Touch" it displays "price", "information" and "mail" that is contains in my database.
Here's a short video of what I'd like to do : http://sendvid.com/whdm4sjf
EDIT
So with the help of Aaron, the code works a little bit better.
In my AS3, the user can choose a "categorie" in order to displays all the titles contains in the categorie.
For that I've put this $categorie = $_POST['categorie'];
in PHP
And this code in AS3
var loader5:URLLoader = new URLLoader();
var varLoader2:URLLoader = new URLLoader;
varLoader2.dataFormat=URLLoaderDataFormat.VARIABLES;
varLoader2.addEventListener(Event.COMPLETE,completeHandler2);
function categorieSelected(evt:Event):void {
var urlReq:URLRequest = new URLRequest ("http://www.brousse-en-folie.com/sondage/convertXML.php");
urlReq.method = URLRequestMethod.POST;
var urlVars:URLVariables = new URLVariables();
urlReq.data = urlVars;
trace("typeSelected");
urlVars.categorie = evt.target.value;
varLoader2.load(urlReq);
loader5.load(new URLRequest("http://www.brousse-en-folie.com/sondage/convertXML.php"));
loader5.addEventListener(Event.COMPLETE, complete);
}
But it seems that with the JSON loading, there is a conflict and I've got this error with the function categorieSelected
Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
Here's a short video of the problem : http://sendvid.com/18rr26rb