I am trying to dynamically create an HTML table with data from a MySQL table. The data in the table is not static and as such the HTML table needs to be updated when the database table is.
Here is my PHP code:
$sql = "SELECT * FROM pendingusers";
$result = $conn->query($sql);
$response = array(); //$result->fetch_all(MYSQLI_ASSOC);
if ($result->num_rows > 0) {
$i = 1;
while($row = $result->fetch_assoc()) //$response[] = $row;
{
$response[$i]['fname'] = $row["firstname"];
$response[$i]['lname'] = $row["lastname"];
$response[$i]['uname'] = $row["username"];
$response[$i]['email'] = $row["email"];
$response[$i]['password'] = $row["password"];
$i++;
}
echo json_encode($response);
} else {
echo "0 results";
}
$conn->close();
?>
Here is my Javascript function to call the PHP file:
function load() {
// var users = null;
$.post(
"Returnsmedb.php",
function (response) {
console.log(response);
}, 'json'
);
}
This returns an object, with three interior objects, each representing a row, like so : Object { 1: Object, 2: Object, 3: Object }. Each interior object has the fields email, fname, lname, password, uname.
I'm not sure where to go from here. I think this needs to converted to a JavaScript array but I have not found great examples. What do I need to do to get this data into a table?
Each row of the table should correspond to one of the interior objects with appropriate field headings. Also, each row needs to have two buttons that will delete it from the table or add the values to another database table.