doufubian3479 2016-10-18 08:29
浏览 29
已采纳

使用PHP数组创建一个数组Javascript

I have a ajax script that returns me an associative array list as follows:

array(24) {  
["id"]=>  
string(4) "3478"  
["joindate"]=>  
string(19) "2016-10-17 21:20:28"  
["ip"]=>  
string(12) "xxx.xxx.xx.xx"  
...  
}

array(24) {  
["id"]=>  
string(4) "3479"  
["joindate"]=>  
string(19) "2016-10-17 21:20:28"  
["ip"]=>  
string(12) "xxx.xxx.xx.xx"   
...  
}

HTML

<thead>
  <tr>
    <th><b><u>key 1</u></b></th>
    <th><b><u>key 2</u></b></th>
    <th><b><u>key 3</u></b></th>
    <th><b><u>key 4</u></b></th>
    <th><b>etc...</b></th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
    <td>value 3</td>
    <td>value 4</td>
    <td>etc...</td>
  </tr>
<tr>
    <td>value 1</td>
    <td>value 2</td>
    <td>value 3</td>
    <td>value 4</td>
    <td>etc...</td>
  </tr>
</tbody>

My current javascript where formation is an ID and methode is my date

data += "data=" +formation+' '+methode; 
$.ajax({ 
        url: "core/display_lead.php", 
        type: "post", 
        data: data, dataType: "html", 
        success : function(code_html, statut){ 
            console.log(code_html); 
        }, 
        error : function(resultat, statut, erreur){ 
            console.log("La requête n'a pas aboutie..."); 
            console.log(resultat); 
            console.log(statut); 
            console.log(erreur);
         } 
});

how do I get the key to creating a table header, then only the value to fill it ?

For the moment I transmits data to Javascript with a simple PHP var_dump, there is a cleaner method, perhaps?

thanks a lot

  • 写回答

4条回答 默认 最新

  • dongyan7876 2016-10-18 08:47
    关注

    So first change your PHP code to send back a JSON respose instead of a var_dump

    <?php
    
        $all = array();
        while ($row = mysql_fetch_array($req, MYSQLI_ASSOC)){ 
             $all[] = $row; 
        }
    
        echo json_encode($all);   
    

    Now in your javascript, tell the .ajax functionality to expect JSON as a response

    data += "data=" +formation+' '+methode; 
    $.ajax({ 
            url: "core/display_lead.php", 
            type: "post", 
            data: data, 
            dataType: "json",           //<-- amended
    
            success : function(data, statut){ 
                console.log(data); 
            }, 
            error : function(resultat, statut, erreur){ 
                console.log("La requête n'a pas aboutie..."); 
                console.log(resultat); 
                console.log(statut); 
                console.log(erreur);
             } 
    });
    

    Now using this and @krasipenkov answer and a little look at the data using the javascript debugger you should be almost all the way there. The data parameter on your success method should now be a javascript native data type i.e an array or an object depending on the data you built in PHP and easy enough to process

    Afraid I really have to add this as well now I see your using the mysql_extension. Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。