php - How can I add rows to this HTML table dynamically...?

856

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?

321

Answer

Solution:

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>"
} ?>
282

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.

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

Source

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.