Bootstrap table, unable to load data from PHP file - php

Here is what I'm trying to do:
I want to fill a bootstrap table using a php file as source. Here is my code:
<table id="non_conformites" data-toggle="table">
<thead>
<tr>
<th data-field="date">Date</th>
<th data-field="conciergerie">Conciergerie</th>
<th data-field="utilisateur">Utilisateur</th>
<th data-field="createur">Créateur</th>
<th data-field="demande">Demande liée</th>
<th data-field="domaine">Domaine</th>
<th data-field="gravite">Gravité</th>
<th data-field="statut">Statut</th>
</tr>
</thead>
</table>
<script>
$(function () {
$('#non_conformites').bootstrapTable({
url:'data_ga.php'
});
});
</script>
Here is what my php file echo (I use json_encode() in order to display the right format):
[
{
"date": "2017-11-15 09:21:12",
"conciergerie": "xx_easywhere",
"utilisateur": "99XX01",
"createur": "1",
"demande": "xx_easywhere_15112017_Gard_1",
"domaine": null,
"gravite": "0",
"statut": "3"
},
...more data...
{
"date": "2017-11-16 17:07:43",
"conciergerie": "xx_easywhere",
"utilisateur": "99XX01",
"createur": "",
"demande": "xx_easywhere_16112017_Lava_5",
"domaine": "test_maj",
"gravite": "1",
"statut": "2"
}
]
But nothing is displayed in my table. Actually, I know that my php file is never visited, because if it is, it should create some debug txt files, and it's not the case.
When I put the data above directly inside the and use it as a source for my table, it works.
So I'm probably not using my php source properly. If you have any clue about what I'm doing wrong, I'm more than interested :)

Related

How can I write large PDFs quickly and efficiently using mPDF?

I am updating an application using an old version of fPDF to the latest mPDF version 8.
My question is how to mitigate the much discussed slowness with HTML tables in mPDF.
The current fPDF code uses the Cell method and generates the PDF in less than a second.
By comparison, writing some simple HTML to use in mPDF generates a similar PDF in about 12-15 seconds.
My testing is with around 3500 records, but this can easily hit 10k records for some results.
Loading
I am loading the mpdf object like this;
$mpdf = new \Mpdf\Mpdf(array(
"tempDir" => "/tmp/pdf",
"mode" => "utf-8",
"format" => "Letter-P",
"margin_left" => "10",
"margin_top" => "10",
"margin_right" => "10",
"margin_bottom" => "10"
));
$mpdf->WriteHTML(file_get_contents("css/pdf.css"), \Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->simpleTables = true;
My css/pdf.css file
.cdrTable {
width: 25cm;
}
.cdrTable td {
text-align: center;
}
Writing
I've kept the HTML as simple as possible to prevent any auto-resizing issues.
Store this html in variable and write once using $mpdf->WriteHTML($html);
<h2>Results for this run</h2>
<p><strong>Sources are:</strong>
[pbx] From PBX
[sdc] From DC
</p>
Store a table of 50 records and write once using $mpdf->WriteHTML($html);
<table class='cdrTable'>
<tr>
<th>#</th>
<th>Call Started</th>
<th>Call From</th>
<th>Call To</th>
<th>Duration</th>
<th>Ext</th>
<th>Ext Label</th>
<th>Data Src</th>
</tr>
<tr>
<td>1</td>
<td>02-07-2020 04:52:39 PM</td>
<td>Polycom VVX601 [11]</td>
<td>Conference [86]</td>
<td>0:13</td>
<td>101</td>
<td>My Phone</td>
<td>pbx</td>
</tr>
.. Up to 50 records ..
</table>
Store this html in variable and write once using $mpdf->WriteHTML($html);
<table>
<tr>
<td>3555 records matched</td>
<td>Total: 1965:11</td>
</tr>
</table>
Finally
$mpdf->Output("reports.pdf", \Mpdf\Output\Destination::DOWNLOAD);
Conclusion
Outputting this to a browser is quick;
Peak Mem: 6.82 M, Time: 0.055243015289307
Generating the PDF is not so quick;
Peak Mem: 14.52 M, Time: 13.560908079147
I have tried writing 100, 200, 1000, and the whole result set at once without much difference in time.
Is there anything I can do to make this quicker?
Should I use something besides HTML tables or skip using HTML at all?

Locating TD position by it's TH, domCrawler

I am trying to scrape table's td tag, but first I need to check th. For example let say table structure is like below.
<tbody>
<tr>
<th>color</th>
<td>red</td>
</tr>
<tr>
<th>price</th>
<td>23.267$</td>
</tr>
<tr>
<th>brand</th>
<td>mustang</td>
</tr>
</tbody>
In this table I need to scrape mustang value. But I can't use $crawler->filter('table td')->eq(3); for that. Because position is always changing. So I need to catch the value by it's th. I mean if th's value is brand then get it's td
what is the best way to this?
Not sure it's a best solution, but I solved it with this:
$props = $node->filter("table th")->each(function($th, $i){
return $th->text();
});
$vals = $node->filter("table td")->each(function($td, $i){
return $td->text();
});
$items = [
"brand" => "",
"color" => "",
];
for ($a=0; $a < count($props); $a++) {
switch ($props[$a]) {
case 'brand':
$items["brand"] = $vals[$a];
break;
}
}
If there is another way or much better way to achieve this. Please feel free to post it here. Thank you.

How to write content by calling WriteHtml() function twice at a same file using mpdf library in PHP 7?

I wanna write a table with the records from my database into PDF using mpdf. But when i retrieve the records using while loop my table header takes the effect, the records doing loop as well as table header because the "WriteHtml()" function inside the loop.
I've tried to solve this problem by calling "WriteHtml()" twice, which means to write the table header and content separtely, but the mpdf produces a blank page PDF file.
$html = "<table border='0' width='100%' cellspacing='0'>
<tr>
<th>ID</th>
<th>NAMA</th>
<th>PEKERJAAN</th>
<th>ALAMAT</th>
<th>SUKU</th>
</tr>";
$mpdf->WriteHTML(utf8_decode($html),\Mpdf\HTMLParserMode::DEFAULT_MODE, true, false);
while ($data = mysqli_fetch_array($mysqli_query)) {
$html2 = "<tr>
<td align='center'>".htmlspecialchars($id++)."</td>
<td align='center'>".htmlspecialchars($data['nama'])."</td>
<td align='center'>".htmlspecialchars($data['pekerjaan'])."</td>
<td align='center'>".htmlspecialchars($data['alamat'])."</td>
<td align='center'>".htmlspecialchars($data['suku'])."</td>
</tr>
</table>";
$mpdf->WriteHTML(utf8_decode($html2),\Mpdf\HTMLParserMode::DEFAULT_MODE,false, true);
}
$mpdf->Output();
I expect the content of PDf file output is the table like my index.php like this:
https://photos.smugmug.com/Stackoverflow/i-26sKBgt/0/ed83bc1c/L/expect_output%20-%20Copy-L.png
instead of like this:
https://photos.smugmug.com/Stackoverflow/i-cVszxJW/0/af15bde0/L/unexpect_output%20-%20Copy-L.png
i am sorry i post a link because i am not being able to post image right now

Bootstrap TableExport decimal issue

I have a bootstrap table displaying employee data, including the payroll ID which have the given format: 1606.xxxx
Here's what my table looks like :
<table id="table_search"
data-toggle="table"
data-search="true"
data-show-refresh="true"
data-show-toggle="true"
data-show-columns="true"
data-show-export="true"
data-minimum-count-columns="2"
data-show-pagination-switch="true"
data-pagination="true"
data-page-list="[10, 25, 50, 100, ALL]"
data-show-footer="false"
data-export-data-type="all"
data-export-types="['excel']">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="payroll_id" >Payroll ID</th>
<th data-field="nama_karyawan">Employee Name</th>
<th data-field="level">Level</th>
<th data-field="grade">Grade</th>
<th data-field="title">Title</th>
<th data-field="lokasi">Location</th>
<th data-field="cost_sales">Cost Sales</th>
<th data-field="dept">Department</th>
<th data-field="div">Division</th>
<th data-field="dir">Directorat</th>
<th data-field="active_period">Active Period</th>
</tr>
</thead>
</table>
The table displays it correctly, however when I export it into excel using TableExport plugin it goes like this
exported results
As you can see, somehow the plugin treats it as a number with decimal, which is exactly what I am avoiding. I've tried commenting the parseNumber function which might be the cause in the tableExport js file, however the results always comes out the same
What am I doing wrong ?
PS: I don't want formatting after the file is exported, I want it to export the data as is.
You could use
...
<td data-tableexport-msonumberformat="\#">123.450</td>
...
With this the tableExport.js will use mso-number-format: "\#" as TDstyle while creating the HTMLExcel export. This results in formatting the cell as Text.
Example:
http://jsfiddle.net/uqtubq5c/1/
You could also use
...
<td data-tableexport-msonumberformat="0.000">123.450</td>
...
This leads to the number format 0.000 in Excel. So the cell content remains a number and will be useable in calculations further. The Text will possible lead to issues while used in calculations.
If you cannot set own data-tableexport-msonumberformat attributes to TD elements, then you could extend the tableExport.jquery.plugin.
In tableExport.js have:
...
var defaults = {
onMsoNumberFormat: onMsoNumberFormat,
...
onMsoNumberFormat must be a function.
If the function onMsoNumberFormat is like:
onMsoNumberFormat = function(cell, row, col) {
if (row > 0 && col == 2) {
return "#\\,##0\\.00";
}
if (row > 0 && col == 3) {
return "\\#";
}
};
then the third column (col==2) from row 2 (row>0) upwards will get style="mso-number-format:#\,##0\.00" and the fourth column (col==3) from row 2 (row>0) upwards will get style="mso-number-format:\#". # is Textformat.
Example:
http://jsfiddle.net/uqtubq5c/3/

How to unite 3 files in one table?

I have a test script, that generate txt file with answers. And have 2 txt files with the correct answers. I want:
1) Unite all files in one table, like:
<table>
<tr>
<td>№ of question</td>
<td>data from file 1</td>
<td>data from file 2</td>
<td>data from file 3</td>
</tr>
...
</table>
2) I want to replace id in this files on text from DB (MySQL). I have table with question and answers with similar id (like in txt files).
All files have structure like:
1|3
2|4
3|1
where first number - is id of a question, and second is a variant of answer.
I start coding, but don't know how to include data from files:
// Slect from DB
$qsel=mysql_query("SELECT `qid`, `qtext` from `questions` ORDER BY `qid`");
// Open file 1
$key1=fopen("data/test_1_key1.txt", "r");
$k1=explode("/r/n", $key1);
// Open file 2
$key2=fopen("data/test_1_key2.txt", "r");
$k2=explode("/r/n", $key2);
$rtable='<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="40%">Q</th>
<th width="20%">A 1</th>
<th width="20%">A 2</th>
<th width="20%">NAME</th>
</tr>';
while($q=mysql_fetch_row($qsel))
{
$rtable.='<tr><td><b>'.$q['1'].'</b></td>';
$rtable.='<td>data from file 1</td>';
$rtable.='<td>data from file 2</td>';
$rtable.='<td></td>';
}
echo '</table>'.$rtable;
I would first fetch the textfiles and convert it into indexed array:
$tmp1 = file('text1.txt');
$data1 = array();
foeach($tmp1 as $line)
{
list($key1, $val1) = explode("|", $line);
$data1[$key1] = $val1;
}
and then, on mysql fetch loop, just use the indexed array:
while($q=mysql_fetch_row($qsel))
{
$rtable.='<tr><td><b>'.$q['1'].'</b></td>';
$rtable.='<td>' . ( isset( $data1[ $q['0'] ] ) ? $data1[ $q['0'] ] : '' ) . '</td>';
$rtable.='<td>data from file 2</td>';
$rtable.='<td></td>';
}

Categories