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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?