Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding HTML table from JSON object via php URL
    primarykey
    data
    text
    <p>I'm having issues building an html table with a JSON object that I've already got generated by a php page.</p> <p>I'm building my JSON object from a spreadsheet where it includes: Fname, Lname, Ext, Rm.</p> <p>My json.php webpage gives me this result:</p> <pre><code>[{"fName":"John","lName":"Doe","Ext":"#666","Rm":"C3","id":0},{"fName":"Abraham","lName":"Lincoln","Ext":"#917","Rm":"C1","id":1}] </code></pre> <p>So now I'm trying to build an html page filling a table with this data using jquery. Here's what I've got for my index.html:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="stuff" &lt;table id="userdata" border="1"&gt; &lt;thead&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;th&gt;Ext&lt;/th&gt; &lt;th&gt;Room&lt;/th&gt; &lt;/thead&gt; &lt;tbody&gt;&lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;script&gt; $(document).ready(function(){ $("#userdata tbody").html(""); $.getJSON("json.php", function(data){ $.each(data.members, function(i,user){ var tblRow = "&lt;tr&gt;" +"&lt;td&gt;"+user.fName+"&lt;/td&gt;" +"&lt;td&gt;"+user.lName+"&lt;/td&gt;" +"&lt;td&gt;"+user.Ext+"&lt;/td&gt;" +"&lt;td&gt;"+user.Rm+"&lt;/td&gt;" +"&lt;/tr&gt;" $(tblRow).appendTo("#userdata tbody"); }); } ); }); &lt;/script&gt; </code></pre> <p> </p> <p>EDIT: Found my solution with the following code:</p> <pre><code>&lt;?php $json = file_get_contents('collab.json.php'); $data = json_decode($json,true); echo '&lt;table&gt;'; echo '&lt;tr&gt;&lt;th&gt;Username&lt;/th&gt;&lt;th&gt;Period&lt;/th&gt;&lt;th&gt;Room&lt;/th&gt;&lt;th&gt;Next?&lt;/th&gt;&lt;/tr&gt;'; $n = 0; foreach ($data as $key =&gt; $jsons) { foreach ($jsons as $key =&gt; $value) { echo '&lt;tr&gt;'; echo '&lt;td&gt;'.$data[$n]['username'].'&lt;/td&gt;'; echo '&lt;td&gt;'.$data[$n]['period'].'&lt;/td&gt;'; echo '&lt;td&gt;'.$data[$n]['Rm'].'&lt;/td&gt;'; echo '&lt;td&gt;'.$data[$n]['next'].'&lt;/td&gt;'; echo '&lt;/tr&gt;'; $n++; } } echo '&lt;/table&gt;'; ?&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload