duandunzhuo3234 2016-01-12 20:52
浏览 53
已采纳

使用PHP在AS3中创建链接

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

  • 写回答

1条回答 默认 最新

  • doufeng3602 2016-01-13 17:28
    关注

    Here's a rough idea of what I would do:

    1. Return all your data as XML or JSON. For example you can use PHP json_encode to return the SQL results as JSON.

    2. Load the JSON into AS3 using URLLoader and JSON.parse.

    3. Now you have an Array of data you can iterate over and display list items.

    4. Add a click handler to each display list item that will hide the list, and show a detailed view.

    You can accomplish this many ways, here's one simple example:

    PHP

    $products = array();
    
    while ($row = mysql_fetch_array($sql_result)) {
        $products[] = array(
            "title" => $row["theTitle"],
            "price" => $row["thePrice"]
        );
    } 
    
    echo json_encode($products);
    

    Which should output valid JSON like this:

    [
        {
            "title": "Product 1",
            "price": 100
        },
        {
            "title": "Product 2",
            "price": 200
        },
        {
            "title": "Product 3",
            "price": 300
        }
    ]
    

    AS3

    Load the JSON and render it out:

    var products:Array;
    
    var list:Sprite = new Sprite();
    addChild(list);
    
    var details:TextField = new TextField();
    addChild(details);
    
    var loader:URLLoader = new URLLoader();
    loader.load(new URLRequest("products.php"));
    loader.addEventListener(Event.COMPLETE, complete);
    
    function complete(e:Event):void {
        products = JSON.parse(loader.data) as Array;
    
        for(var i:int = 0; i < products.length; i++){
            createListItem(i, products[i]);
        }
    
        showList();
    }
    
    function createListItem(index:int, item:Object):void {
        var listItem:TextField = new TextField();
        listItem.text = item.title;
        listItem.y = index * 20;
        listItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
            showDetails(item);
        });
        list.addChild(listItem);
    }
    
    function showList():void {
        details.visible = false;
        list.visible = true;
    }
    
    function showDetails(item:Object):void {
        list.visible = false;
        details.visible = true;
    
        details.text = "Price: " + item.price + "
    Information: " + item.information + "
    Mail: " + item.mail;
    }
    

    This is just a rough start to give you an idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么