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; 

<?php foreach ($records as $data) { ?>

My table looks like this:

<table class="leaderboard">
<th>ID</th><th>First Name</th><th>Last Name</th><th>Robot Name</th><th>Power Remaining</th><th>Level</th>
<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>



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?




Make the<td> in theforeach with a simple echo in PHP, its should work.

For example:

<?php foreach ($records as $data) {
  echo "<td>  escape($data->id) ... </td>"
} ?>



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.

People are also looking for solutions to the problem: php - How to put 'is not null' parameter in Laravel 4 validator for uniqueness


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.