Copy link to clipboard
Copied
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 :
while (
$row = mysql_fetch_array($sql_result))
{ $theTitle = $row["theTitle "];
$phptheTitle= $phptheTitle.' </br> '.$theTitle ;
echo "phptheTitle=" . $phptheTitle;
}
AS3 code :
var myResult:String = evt.target.data.phptheTitle;
output_txt.htmlText = myResult;
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
Copy link to clipboard
Copied
Hi, you can easily construct an array or an object in your PHP code and give it back to your flash/air app using JSON.
PHP Code :
$obj = array();
while(
$row = mysql_fetch_array($sql_result) )
{
$datas = array();
$datas['title'] = $row["theTitle"];
$datas['price'] = $row["thePrice"];
//... add all datas you want
$obj[] = $datas;
}
echo json_encode( array('products' => $obj) );
AS3 code :
var myResult :String = evt.target.data;
var datas :Object = JSON.parse( myResult );
var products :Array = datas && datas.products ? datas.products : [];
// now you have all your products information in the products Array
// lets trace products informations
var len:int = products.length;
for( var i:int = 0; i<len; ++i )
{
trace( products.title, products.price );
}
Copy link to clipboard
Copied
Thank you very much for this complete answer ! I've just two questions :
First, when I test this code, I've code this
error : SyntaxError: Error #1132: Invalid JSON parse input.
What do you think is the problem ?
Secondly, if I want to display only "price" of the product on which I clicked (and not all the prices of all the products), how can I do that ?
Thx again,
Copy link to clipboard
Copied
1 - Please add a trace of evt.target.data, it would be better for seeing what's wrong
2 - Here a simple example how to show the good data for the good text clicked:
var myResult :String = evt.target.data;
var datas :Object = JSON.parse( myResult );
var products :Array = datas && datas.products ? datas.products : [];
// now you have all your products information in the products Array
// create a vector of TextField for keeping your products labels somewhere
var labels:Vector.<TextField> = new <TextField>[];
// looping into all products to create the labels
var len :int = products.length;
var txt :TextField;
for( var i:int = 0; i<len; ++i )
{
// create the textfield
txt = new TextField();
txt.width = 200;
txt.height = 40;
// set the label
txt.text = products.title;
// place it under the previous
txt.y = i*45;
// add it to the stage
addChild( txt );
// add MouseEvent.CLICK listener
txt.addEventListener( MouseEvent.CLICK, onProductClick );
// add it to the texts vector
labels.push( txt )
}
// the on product click listener
function onProductClick( e:MouseEvent ):void
{
// get the id of the product in the texts vector
var id:int = labels.indexOf( e.target );
// verify that id is found
if( id != -1 )
{
// get the price of this product
var price:String = products[id].price;
// now you have the price you can do what you want with it
trace('item', id,'selected : ', products[id].title, price);
}
}
I hope this could help you