Reputation: 51
I want to update my generated table in my php file only when there are changes in the database table but i don't know how to do this. After reading all the many similar questions i still haven't come close to understanding what it is that I am supposed to do.
Im displaying my table within this div <div id="table_holder" class="table-responsive" style="height: 300px;"></div>
within my main.php
and this is the JS that I'm using to refresh the table every 5 seconds.
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#table_holder').load('live_table_data.php', function(){
setTimeout(refreshTable, 5000);
});
}
</script>
This is how im displaying my table in live_data_table.php
$result = $conn->query("select date,student_id, s.s_name as Name, s.s_lname as Last, s.s_email as Email, time from attendance_record A
inner join student s on A.student_id = s.s_id
inner join session b on A.session_id = b.session_id
where b.module_id = 7 and A.date = '2017-03-20' and b.start_time = '16:00'");
?>
<table class="table-responsive" id="live_table">
<thead>
<tr >
<th>Student ID</th>
<th>Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Arrival</th>
<th>Date</th>
</tr>
</thead>
<tbody style="overflow: auto">
<?php
while ($row = $result->fetch_assoc()) {
unset($student_id, $Name, $Last, $Email, $time, $date);
$student_id = $row['student_id'];
$Name = $row['Name'];
$Last = $row['Last'];
$Email = $row['Email'];
$time = $row['time'];
$date = $row['date'];
echo "<tr>";
echo "<td>".$student_id."</td>";
echo "<td>".$Name."</td>";
echo "<td>".$Last."</td>";
echo "<td>".$Email."</td>";
echo "<td>".$time."</td>";
echo "<td>".$date."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
My problem is that I've no idea how to dynamically update the table based on if and only if there is a change within the database
Upvotes: 2
Views: 1593
Reputation: 51
As per the recommendations of many, I decided to go with a different route where by eliminating the refresh of the table every 5 seconds and instead have a button designed to refresh the amount of rows within the table.
<button type="button" onClick="refreshTable()">Refresh</button>
<div id="table_holder" class="table-responsive" style="height: 300px;"></div>
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#table_holder').load('live_table_data.php')};
Upvotes: 0
Reputation: 1185
Usually people don't do what you are trying to, because making a query which would tell if anything has changed would require the same amount of resources as doing the original query. So the best solution might be to just repeat the query in a fixed interval, like in every minute.
But in case you have a very heavy query, for which you want to reduce the number of times it is done, you can do the following:
Since traditional relational databases have no event-driven aspects, the only way to do this, is to make polls for the changes in fixed intervals. So this as to be solved on the application level, namely: every time you modify the data you update something in the database with the current datetime. There are many possibilities:
You could add a last_modified
field to the student
table, and each time the code adds or modifies a record it would set there the current time. Then you can either query only the modified records, or check the latest modification time by:
select
max(`last_modified`)
from
`student`
You can create a separate table, which has a record for each tables you want to track. Every time your code adds or modifies a student, then it would set the current time for the student table.
insert into
`track_updates`
(`table_name`, `time`)
values
("student", "2017-03-21 12:59:00")
on duplicate key update
`time` = "2017-03-21 12:59:00"
And then query by the table:
select
`time`
from
`track_updates`
where
`table_name` = "student"
You could even have a table with always a single record in it, containing a single field for the last update time, for all relevant tables.
You can make these kinds of polling more efficient, with techniques like: https://en.wikipedia.org/wiki/Comet_%28programming%29
Upvotes: 1
Reputation: 992
You can't update content in browser without sending request from frontend. But you can minimize this process. Just create php script, which will output status and result in json. To make php script understand, when table updated and when not, use hash summ.
data-hash="<some hash>"
to your table tag.However, maybe it is not worth it and you better to leave it as is.
Upvotes: 0
Reputation: 147
To know if there has been modification on your table, I can think of :
There might be some other ways to do this.
Upvotes: 0