php - How can I add rows to this HTML table dynamically...?
I am making a game for class and I have a leaderboards page. What I am wanting to do is dynamically add a new row to the table every time a new result is added to the database.
This is my PHPSELECT *
:
<?php $records = array();
if ($results = $db->query("SELECT * FROM user_settings, leaderboards")) {
if ($results->num_rows) {
while ($row = $results->fetch_object()) {
$records[] = $row;
}
$results->free();
}
}
?>
<?php foreach ($records as $data) { ?>
...
My table looks like this:
<table class="leaderboard">
<tr>
<th>ID</th><th>First Name</th><th>Last Name</th><th>Robot Name</th><th>Power Remaining</th><th>Level</th>
</tr>
<tr>
<td><?php echo escape($data->id); ?></td><td><?php echo escape(htmlentities($data->first_name)); ?></td><td><?php echo escape(htmlentities($data->last_name)); ?></td><td><?php echo escape(htmlentities($data->robot_name)); ?></td><td><?php echo escape($data->power_remaining); ?></td><td><?php echo escape($data->level) ?></td>
</tr>
<tr>
</tr>
</table>
This successfully grabs the value from the database however how can I dynamically add a new record to the HTML table every time a new value is entered into the database?
Answer
Solution:
Make the
<td>
in theforeach
with a simple echo in PHP, its should work.For example:
Answer
Solution:
You need to check current result count with Ajax every some time period, for ex. every minute. If there is more results - send them to client side and add them to table with javascript.