Update html table at a specific interval - php

I am new to PHP and Ajax. In my project I need to update a html table (for example each row of the table will contain the room name and current room temperature. I need to update the temperature values in each row at a specific interval).
The backend MySQL table will contain the current temperature values of each rooms. The webpage is based on bootstrap3. The number of rows is dynamically generated by my PHP code.
How can I update these values (each row) at a specific time interval?

This is how
Jquery
setInterval(function(){
$.get("get_my_updated_values.php" , function(result){
$("#my_table_tbody").html(result); // my_table_tbody is the id of the body of your table.
});
}, 3000);
HTML
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody id = "my_table_tbody">
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
</tbody>
</table>
PHP (get_my_updated_values.php)
<?php
// some php to retrieve the data from your database
echo '<tr>
<td>New data 1</td>
<td>new data 2</td>
</tr>';
?>
it will refresh after every 3 seconds ... Its the exact thing you want ... Hope that helps

I use the following to update certain aspects of my web page:
<div id="header-wrapper"><span class="pull-right label label-default" id="header-count"><?php echo $count; ?></span></div>
<script>
$(document).ready(function(){
var $header = $("#header-wrapper");
setInterval(function () {
$header.load("test.php #header-count");
}, 10000);
});
</script>

Related

Bootstrap table not recognizing rows generated using AJAX

For reference :
I'm working with Bootstrap-table using Bootstrap v3.3.6
I'm trying to populate my table using AJAX and jQuery, but the problem is that when i do so none of bootstrap extensions seems to work with those rows.
I'm working on a data table using some extensions as the following :
`<table id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true" data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar">
<thead>
<tr>
<th data-field="state" data-checkbox="true"></th>
<th>ID</th>
<th>ISBN</th>
<th>Description</th>
</tr>
</thead>
<tbody id="books-data">
<!-- table data rows to be inserted here -->
</tbody>
`
If i'm filling the table using PHP everything seems to be working fine, by 'everything' i mean displaying data as well as the automatically added attributes to table rows as adding a chekbox in the first <td> and data-index attribute for every column in every row.
However, filling the table using AJAX only fills the table with data and there is no interaction between the table and its rows, no bootstrap-table attributes or checkboxes are added as well as no extensions seems to be working with those rows (for example export, rows selection ...)
For more detail :
When embedding PHP code into table columns:
source code :
while ($row = mysqli_fetch_array($result)){ ?>
<tr>
<td></td>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['isbn']; ?></td>
<td><?php echo $row['description']; ?></td>
</tr>
<?php } ?>
Generated code by bootstrap-table:
<tr data-index="0">
<td class="bs-checkbox ">
<input data-index="0" name="btSelectItem" type="checkbox">
</td>
<td style>
<!-- data -->
</td>
<td style> <!-- the same thing... --> </td>
</tr>
When generating rows dynamically using AJAX :
<tr>
<td></td>
<td><!-- data --></td>
<td><!-- data --></td>
<td><!-- data --></td>
</tr>
jQuery function used :
$(function()
{
$.ajax({
url:"fetch_books.php",
method:"POST",
dataType:"json",
success:function(data)
{
for(let count=0; count<data.length; count++)
{
let html_data = '<tr><td></td>';
html_data += '<td>'+data[count].id_livre+'</td>';
$('#books-data').append(html_data);
}
}
})
});
I want the result to be the same as when i embed php inside each column in my table, i don't even know why i'm having this issue, i need some help.
Have you tried using their method for loading JSON instead of your own jQuery?
From: https://bootstrap-table-docs3.wenzhixin.net.cn/getting-started/#usage-via-javascript
"We can also use remote URL data by setting url: 'data1.json'.
$('#table').bootstrapTable({
url: 'data1.json',
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}, ]
});
<table id="table"></table>
If you don't want to use their supplied method then maybe try adding the classes and data-index etc. that you see in the bootstrapped table to the table you're generating...
let html_data = '<tr><td></td>';
=>
let html_data = '<tr data-index="0"><td class="bs-checkbox "><input data-index="0" name="btSelectItem" type="checkbox"></td>';
though I don't think that will get you working... it should at least style things correctly. You'll need to use a way that gets everything pulled into the DOM correctly. I'd try using Bootstrap-table's method for loading JSON they have supplied above. You might have to create a PHP script that takes some arguments and returns properly formatted JSON for their method to work.

Datatable sorting asc icon still appear on first column even being disabled

Hi I have the following table design. I dont want the first and last column to be sortable. I have set accordingly.But the icon asc still appears on the first column but not on the last column. But when I try to sort the second column it goes off.
<table id="userTable" name='userTable' class="table table-striped table-bordered bootstrap-datatable " data-column-defs='[{"sortable": false, "targets": [0,3]}]' style='table-layout: fixed;'>
<thead>
<tr>
<th class="no-sort" style='width: 10%;'>No.</th>
<th style='width: 25%;'>First Name</th>
<th style='width: 20%;'>last Name</th>
<th style='width: 25%;'>Details</th>
</tr>
</thead>
</table>
Even if you disable sorting for a column, the dataTables sort order still remain. By default order is [0, 'asc']; simply set order to target the #2 column instead :
var table = $('#example').DataTable({
//....
order: [[ 1, "asc" ]] //column indexes is zero based
})
demo -> http://jsfiddle.net/6k26o7mk/
Or use order: [] if you not want any default order at all (icons will be hidden until the user sort the table).
In version 1.10.20 it worked for me just like this:
$(document).ready(function(){
$('#example').DataTable({
ordering: false, //disable ordering
initComplete: function( settings, json ) {
$("th").removeClass('sorting_desc'); //remove sorting_desc class
}
});
});
<link href="//cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<table id="example" class="display">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</tbody>
</table>
I was not able to get this working on datatables version: 1.10.11 with the DataTables download builder
Being that case, I used jQuery as a workaround to remove the sorting class, which in turn will remove the icon.
$(document).ready(function () {
$('#my-dataTables').DataTable({
paging: false,
ordering: false,
info: false,
});
// Removes icon
$('.sorting_asc').removeClass('sorting_asc');
});

Display SQL data for a specific row onclick

I currently have 2 tables, top and bottom. For the top, there would be rows of data I had called out from my SQL database. As for the bottom, the data is from the same database as the top table, but displaying different fields. The fields are all in the same row in my SQL database.
Basically, upon click on any row on the top table, the bottom table will show more information which is also within the same row in the SQL database. I'm not sure how to display data specifically for a certain row, for now, when I click on any row on the top table, it displays all the rows in the SQL.
Code for the tables:
<table id="table_id">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($results)) {
?>
<tr data-details="c01" class="itemDetails">
<td><?=$row['name']?></td>
<td><?=$row['address']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<br /><br />
<div>
<table border=1 id="c01" class="aside hidden">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($results2)) {
?>
<tr>
<td><?=$row['product']?></td>
<td><?=$row['quantity']?></td>
<td><?=$row['price']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Code for Jquery:
<script>
$(document).ready(function () {
$('table.hidden').hide();
$('tr.itemDetails').click(function() {
var id = $(this).data('details');
$('table.hidden').hide();
$('#'+id).show();
});
});
</script>
If I understood correctly, this code will help you:
$(function() {
$('table.hidden').css('display','none');
$('tr.itemDetails').click(function() {
var index = $(this).index();
$('table.hidden').css('display', 'block');
$('table.hidden tr').css('display', 'none');
$('table.hidden tr:first-child').css('display','table-row');
$('table.hidden tr:nth-child(' + (index + 1) + ')').css('display','table-row');
});
});
working example: jsfiddle
You almost done on this but it seems that you are trying to hide / show all entire table. So you need to hide / show only specific row instead.
instead of
$('table.hidden').hide();
$('#'+id).show();
It should be updated to
$('table.hidden tr').hide();
$('table.hidden tr #'+id).show();
And your HTML should be
<tbody>
<?php
while ($row = mysql_fetch_array($results2)) {
?>
<tr id="<?=$row['id']?>">
<td><?=$row['product']?></td>
<td><?=$row['quantity']?></td>
<td><?=$row['price']?></td>
</tr>
<?php
}
?>
</tbody>
I hope this guide could help.

jQuery DataTables on live tables from PHP script

I'm having a problem with the implementation of DataTables, I simple can't make it work in a returned table from a PHP script. Suppose this simple PHP script named simple_table.php:
<?php
echo '
<table class="table_type">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</tbody>
</table>';
?>
Now I also have the next very simple jQuery script declared after the </body> tag in the hipotetic html file:
$(document).ready( function () {
$('table.table_type').dataTable();
} );
$('div.push_button li').click(function() {
var content = $(this).closest('div').children('div.content');
$.get('simple_table.php', {}, function(html_table) {
content.html(html_table);
} );
} );
What is needed to be made for this simple example to work and keeping in it simple? The problem is that there is no solution for this useful scenario of a dynamic table made by a so simple script!
Well, maybe you have to have a preexisting <table> and only yhen be able to filled up the respective <tr>...
The problem is because you are running .dataTable() before the page has the element table.table_type. Instead of calling .dataTable() when the page loads, you need to run it once the table has been added to your <div>.
$('div.push_button li').click(function() {
var content = $(this).closest('div').children('div.content');
$.get('simple_table.php', {}, function(html_table) {
content.html(html_table);
content.find('table').dataTable();
} );
} );

How to highlight multiple items with same name in a dynamic management system

Ok, I have a backend system that watches tables update live (made in php) and i'm looking for a method where I can hover over an ID on one table and all the same IDs across tables would highlight also.
I can't have a page with just their ID as the page includes watching other live site data that needs to be watched.
Seeing the HTML you're using would be helpful, but maybe something like (assume the database IDs are in TDs with a class of 'dbid')
(fair warning I did not have time to test this, but it should work or at least convey the idea)
$('td.dbid').hover(function() {
id = $(this).html();
$("td.dbid:contains('" + id + "')").css('background-color','yellow');
},
function () {
id = $(this).html();
$("td.dbid:contains('" + id + "')").css('background-color','none');
});
Sample HTML
<table>
<tr>
<td class="dbid">14</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">14</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">13</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">14</td>
<td>blah</td>
</tr>
<tr>
<td class="dbid">13</td>
<td>blah</td>
</tr>
</table>

Categories