2011-04-25 139 views
1

我在同一个文件夹中有一个table.html,data.php和json.csv。用json值填充html表格

data.php正在做fopen("json.csv","r")从json.csv中读取。

如何在table.html中将json对象显示为表格?

<html> 
    <head> 
    <script type="text/javascript" src="jquery.js"> 
    function display(){ 
    $.post('data.php',function(data){ 
    $("#txtJSON").html(data); 
    }); 
    } 

    </script> 
    </head> 
    <body onload="display()"> 
    <table id="#jobtable"> 
    <input type="text" id="txtJSON" /> 
    </table> 
    </body> 
    </html> 
+3

你有没有试图自己写任何代码?如果是这样,请分享它,并让我们知道它不工作的方式。 – GordonM 2011-04-25 14:45:26

+0

是json.csv JSON还是CSV?他们是两种不同的格式... – ceejayoz 2011-04-25 16:12:41

回答

1

伪码

PHP

Take the data read in from doing the fopen. 
create a php array (a1) 
create a php array (a2) 
split it on new lines 
iterate through all of those lines 
    clear (a2) 
    split the current line on commas (or w/e separator) 
    insert each element into a2 
    insert a2 into a1 

return a json_encode(a1) 

HTML

do a jQuery.parseJSON(returned data from php) http://api.jquery.com/jQuery.parseJSON/ 
iterate through the datastructure adding a new html row for every row in the structure... 
0

你可以转换json.csv输出一个PHP数组,并在该文件中的一个php文件合并data.php & table.html让HTML表格与foreach