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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配