How do I create an HTML table, in jQuery, with JSON data?

Displaying JSON data through jQuery from a PHP file?

  • This is my first post here so, bare with me... I'm also fairly new to jQuery and even more new to JSON... I'm trying to get data to display on my webpage using jQuery from a PHP file that is making JSON. So I have accessed my db and converted to json - in two different ways because I'm not sure which way is best/necessary? These are the two ways <?php require_once("connect.php"); $pages = array(); $sth = mysql_query("SELECT * FROM tbl_page"); while($row = mysql_fetch_object($sth)) { $pages[] = $row; } echo '{"pages":'.json_encode($pages).'}'; ?> and <?php include('connect.php'); $sql=mysql_query("SELECT * FROM tbl_page"); echo '{"page": ['; while($row=mysql_fetch_array($sql)) { $id=$row['page_id']; $title=$row['page_title']; $content=$row['page_content']; echo ' { "id":"'.$id.'", "title":"'.$title.'", "content":"'.$content.'" },'; } echo ']}'; ?> Now I'm having a lot of difficulty putting this information out in my html file. I'd copy and past the code but there is really nothihng there... just empty script tags at the moment. I have tried multiple tutorials and the only one I found and managed to actually understand, apparently no longer works with the new jquery or something? If anyone can help that would be greatly appreciated! Just point me in the right direction? lol

  • Answer:

    What is the issue exactly? Are you having trouble converting the database query results into JSON format or are you having trouble presenting the JSON data (e.g. in an HTML table)? If the former, this stack exchange answer has the logic to encode query results into JSON: http://stackoverflow.com/questions/383631/json-encode-mysql-results If the latter, how are you trying to present the JSON data? Are you just trying to output raw JSON or are you putting the data into some kind of table? If you want to present the data into a regular HTML table, there's no reason to encode the query results into JSON. Just iterate through the results and echo out the proper tags (tr, td, etc.). Or, you can use the encoded JSON and feed it into a nifty jQuery plugin found on datatables.net (although this may be overkill for what you're doing).

Danielle at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.