user544079
user544079

Reputation: 16629

populate html table with json values

I have an table.html, data.php and json.csv within the same folder.

data.php is doing fopen("json.csv","r") to read from json.csv.

How can I display the json objects as a table within table.html?

    <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>

Upvotes: 1

Views: 1288

Answers (3)

Gajus
Gajus

Reputation: 73758

You will be better off if you use JavaScript for any extensive DOM manipulation. More specifically, jQuery plugin JSON-to-table can do exactly what you want.

Upvotes: 0

g19fanatic
g19fanatic

Reputation: 10921

psuedocode

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...

Upvotes: 1

jimy
jimy

Reputation: 4908

you can convert json.csv output in a php array and merge data.php & table.html in one php file in that file make a html table with foreach

Upvotes: 0

Related Questions