jQuery DataTables on live tables from PHP script - php

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();
} );
} );

Related

PHP sscanf doesn't print first line in file but the following lines

I have a bunch of files with data formated as follows:
int|int|string|string
I have some functions to get all files in a directory, format their names and make a select drop-down menu from that.
From that, the user selects the file he wants to open.
The choosen file (its filename) gets send via POST to a second php file that opens in an iframe right below the drop-down form.
These are the relevant contents of said file:
<table>
<thead>
<tr>
<th>A:</th>
<th>B:</th>
<th>C:</th>
</tr>
</thead>
<tbody>
<?php
$fl = $_POST["file"];
$currentfile = fopen("./dir/$fl","r");
if ($currentfile) {
while (($line = fgets($currentfile)) !== false) {
$n = sscanf($line, "%d|%d|%[^|]|%[^\n]", $a,$b,$c,$d);
print "<tr><td>$a</td><td>$b</td><td>$d</td></tr>";
}
fclose($currentfile);
} else {
print "Error: Couldn't open file.<br>";
}
?>
</tbody>
</table>
Now somehow, the first line in each file isn't shown in the table generated by this, everything else is fine.
As an example, here's one file.
1|334|Item 1
2|837|Item 2
3|321|Item 3
4|124|Item 4
5|331|Item 5
etc...
And this is the output I get.
A: B: C:
2 837 Item 2
3 321 Item 3
4 124 Item 4
5 331 Item 5
etc...
Or in code:
<table>
<thead>
<tr>
<th>A:</th>
<th>B:</th>
<th>C:</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>837</td>
<td>Item 2</td>
</tr>
<tr>
<td>3</td>
<td>321</td>
<td>Item 3</td>
</tr>
<tr>
<td>4</td>
<td>124</td>
<td>Item 4</td>
</tr>
<tr>
<td>5</td>
<td>331</td>
<td>Item 5</td>
</tr>
etc...
</tbody>
</table>
So as you can see by above code, the td tags get printed, but there's no data in them, whereas the second set of td tags have the second line of data in them, as it should be, so why doesn't it read, scan and print the first line of the file?
I believe this happens because there is the "Byte order mark" in the beginning of your files.
You need to check for it and replace it or remove it from the files.
Here is what worked for me:
while (($line = fgets($currentfile)) !== false) {
$bom = pack('H*','EFBBBF');
$line = preg_replace("/^$bom/", '', $line);
$n = sscanf($line, "%d|%d|%[^|]|%[^\n]", $a, $b, $c, $d);
print "<tr><td>$a</td><td>$b</td><td>$c</td></tr>";
}
Maybe this code does not work on your system and you need to try some other code to remove the BOM.
Try also this:
$line = str_replace("\xEF\xBB\xBF",'',$line);
Hope it helps.
Reference
What's the difference between UTF-8 and UTF-8 without BOM?

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');
});

Update html table at a specific interval

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>

php Jquery - Server side - Add an edit button

I have a datatable that loads about 20,000 (minimum) numbers (rows of data) . With that said , i use the pipeline feature of jquery datatables since it easily handles a large chunk of data .
The issue is that i want to know how to add a new row dynamically by passing the id to it .
Below is my jquery code :
$(document).ready(function() {
available_numbers();
function available_numbers(){
$('#available_numbers').dataTable( {
"processing": true,
"serverSide": true,
"ajax": $.fn.dataTable.pipeline( {
url: 'fetch_available_numbers.php',
pages: 5
})
} );
}
} );
And below is the table :
<table id="available_numbers" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Phone Number</th>
<th>Assigned To (User Group)</th>
</tr>
<tfoot>
<tr>
<th>Phone Number</th>
<th>Assigned To (User Group)</th>
</tr>
</tfoot>
</table>
The values are fetched in json format , just like the server side script in datatables .
Thanks.

How to put multiple tables in the DataTable jQuery plugin

I've been using this PLUGIN for representing my data from mysql database.
I am using this script EXAMPLE for server side processing.
Now this script is working perfectly why you have one table. But problem is occurring when I have multitable query.
For example I have:
$sWhere = "a.ID=b.ID AND b.Name=c.Name";
This is only where variable. If you go to the link that I gave you can see the php script that is used to fetch data. When I put more then one table I get unique table error. And the search functions can't work.
Can someone show me how to use this script to be able to have multiple tables included in one query.
If you need more source let me know.
EDIT:
My HTML:
<table id="table_my" width="100%" border="1" cellspacing="2" cellpadding="5" class="chart_1">
<thead>
<tr class="even">
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
There should be a change in the HTML Markup, when that multitable query executes. Why don't you compare both and check for the difference in the HTML Markup?
The markup for DataTable jQuery plugin should be this way:
<table>
<thead>
<tr>
<th>Column Name</th>
<th>Column Name</th>
<th>Column Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
</tbody>
</table>
Also remember, it doesn't work with more than one tbody or thead tag and tables without these tags. Also, if you have used colspan and rowspan attributes, it doesn't work.
from www.facebook.com/Heanock Z behere Ethiopia
*
Answer for the above problem
*
i just use like i am using in php or other language and i have two tables to be joined
tbl_employees
**
emp_id
** int(11) auto_increment primary key,
emp_name varchar(50),
sex enum('female','male'),
emp_dept varchar(50),
emp_salary double,
job int(11) = is foreign key that i will share from the second table
tbl_job
**
job_id
** int(11) primary key,
job_name varchar(50),
job_cat varchar950)
lastly here is my code in php mysql plus datatable
Emp ID
Emp Name
Sex
Department
Salary
Job
Edit
Delete
$stmt = $db_con->prepare("SELECT emp_id,emp_name,sex,emp_dept,emp_salary,job_name FROM tbl_employees,tbl_job where job=job_id ORDER BY emp_id DESC");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['sex']; ?></td>
<td><?php echo $row['emp_dept']; ?></td>
<td><?php echo $row['emp_salary']; ?></td>
<td><?php echo $row['job_name']; ?></td>
if you have any question related with above problem write me # heanocklove#gmail.com or My facebook Page listed Above
Thanks very much

Categories