2011-04-25 135 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