I have a PHP file that outputs JSON from the MySQL database, I wanted to get that JSON output into HTML file and display as a table. It works fine when I use the JSON output as it is, but I wanted to take that PHP file as a URL or as a file and get the result as a table in the HTML file.
PHP CODE:
<?php
$DBServer="localhost";
$DBUser="root";
$DBPass="";
$DBName="test";
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
$sql='SELECT userinfo.id, name, user_id, posts FROM userinfo, user_posts WHERE userinfo.id = user_posts.user_id';
$rs=$conn->query($sql);
$data = $rs->fetch_all(MYSQLI_ASSOC);
header('Content-Type: application/json');
echo json_encode($data);
?>
HTML CODE:
<script>
$(document).ready(function() {
// Need to take PHP URL or file name instead of JSON data
var data = {
"report": [{
"id": "Abril",
"name": "13",
"user_id": "Lisboa",
}]
};
// Loop through data.report instead of data
for (var i = 0; i < data.report.length; i++) {
var tr = $('<tr/>');
// Indexing into data.report for each td element
$(tr).append("<td>" + data.report[i].id+ "</td>");
$(tr).append("<td>" + data.report[i].name+ "</td>");
$(tr).append("<td>" + data.report[i].user_id+ "</td>");
$('.table1').append(tr);
}
});
</script>
<div class="container">
<div class="row">
<div class="table-responsive">
<table class="table1 table" >
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>USER ID</th>
</tr>
</thead>
</table>
</div>
</div>
</div>