how to write html table in .txt file using php - php

I want to export datatable records in .txt file and am using doing this with file handling function but when I am trying to create table in txt file it will give me the following output:
by doing this
$fileName = 'filenametxt'.date('Y_m_d_H_i_s').".txt";
$filePath = $upload_path.'/file_folder_txt/';
$fileNameWithPath = $filePath.$fileName;
$txtHeaderData .= '<table id="" class="uk-table uk-table-hover uk- table-striped uk-table-condensed" border="0" cellpadding="5">
<tr>
<th>Location</th>
<th>Filed Name</th>
<th>Length</th>
<th>Description of Fields</th>
</tr>
';
$txtHeaderData .= '</table>';
$handle = fopen($fileNameWithPath, "w");
fwrite($handle, $txtHeaderData);
but the expected output is:
can some please help me that how I can achieve this?

The table you see/want is just a default HTML table, styled by the browser. TXT files only hold text no styling, which your file seems to be doing pretty good.
You could write the output as a CSV file, which would allow people to view the file in Excel (basically on big table)
$fp = fopen('output.csv', 'w');
$fields = array("location", "field name", "length", "description");
fputcsv($fp, $fields);
fputcsv($fp, $fields);
I haven't had time to test this code, but according to the docs it should work.
You could also write your own txt table, tho I do not recommend this since some columns will always be larger than others.
$csvHeaderData = "----------------------------------------" . PHP_EOL;
$csvHeaderData .= "| Location | Field name | Length | Description |" . PHP_EOL;
$csvHeaderData .= "---------------------------------------";

Related

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

php mysql export should create dropdown

I'm trying to make a .xls export from a part of my mysql database. I came up with the following sollution:
//Get the data from database and write it to a table
$siteNumber = 999;
$results = Db::getInstance()->queryResults('SELECT * FROM `configurations`');
?>
<table border="1">
<tr>
<th>Configuration id</th>
<th>Configuration category</th>
<th>Configuration name</th>
</tr>
<?php
foreach($results as $result => $key) {
$str = '<tr>';
$str .= '<td>'.$key->configuration_id .'</td>';
$str .= '<td>'.$key->configuration_category .'</td>';
$str .= '<td>'.$key->configuration_name .'</td>';
$str .= '</tr>';
echo $str;
}
?>
</tr>
</table>
//And I use header to create a excelfile
<?php
// Add data table
include 'exceldata.php';
// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=test.xls");
?>
This all works fine and outputs a nice table. What I'm trying to achieve is all results get exported classified by configuration_category as dropdown.
Is there any way to do this?
Thanks in advance!
I'm not sure i understand. ButIf you want to exprt the exelfile with included dropdown you can just make use of html with unordered list
I guess this: https://github.com/PHPOffice/PHPExcel is what you need.
PHPExcel will allow you to create beautiful spreadsheets and export them into Excel format.
You can create Excel DropDowns using this library as well. See this page for details: Read the list of options in a drop-down list - Phpexcel

How to create .csv text to table in PHP

Hello any ideas or suggestion how to make conversion my .csv text to table?
Check this link for reference: http://vis.stanford.edu/wrangler/app/
You can browse your .csv with fgetcsv and use foreach to browse the array returned. You simply displays the result.
Here is an exemple :
// Open the file with PHP
$oFile = fopen('PATH_TO_FILE', 'w');
// Get the csv content
$aCsvContent = fgetcsv($oFile);
// HTML Table
echo '<table>';
// Browse your csv line per line
foreach($aCsvContent as $aRow) {
// New table line
echo '<tr>';
// Browse your line cell per cell
foreach($aRow as $sContent) {
// New cell with the content
echo '<td>'.$sContent.'</td>';
}
// End the line
echo '</tr>';
}
// Close the HTML Table
echo '</table>';
// Close you file
fclose($oFile);
I have done this 3 weeks ago ^_^
If you have a problem, tell me. Maybe I can help you !

Getting variables from SQL Server for mPDF

I'm using the mPDF class to output a pdf of data from a PHP file. I need to loop through a SQL Server query, save as new variables and write into the $html so it can be outputted to the pdf. I can't place it in the WriteHTML function because it does not recognize PHP code. I need the contents of the whole array so I can't just print one variable.
I have two files:
pdf-test.php:
This file gathers session variables from other php files that are included and reassigns them, so I can use them in the $html.
<?php
// Include files
require_once("form.php");
require_once("configuration.php");
session_start();
$html = '
<h3> Form A </h3>
<div>
<table>
<thead>
<tr>
<th colspan="3">1. Contact Information</th>
</tr>
</thead>
<tr>
<td> First Name: </td>
<td> Last Name: </td>
</tr>
<tr>
<td>'.$firstName.'</td>
<td>'.$lastName.'</td>
</tr>
.
.
.
</table>
';
echo $html;
pdf-make.php:
This file holds the code to actually convert the contents of pdf-test.php into a pdf.
<?php
// Direct to the mpdf file.
include('mpdf/mpdf.php');
// Collect all the content.
ob_start();
include "pdf-test.php";
$template = ob_get_contents();
ob_end_clean();
$mpdf=new mPDF();
$mpdf->WriteHTML($template);
// I: send the file inline to the browser.
$mpdf->Output('cust-form-a', 'I');
?>
This is my loop:
$tbl = "form_Customers";
$sql = "SELECT ROW_NUMBER() OVER(ORDER BY custFirt ASC)
AS RowNumber,
formID,
custFirt,
custLast,
displayRecord
FROM $tbl
WHERE formID = ? and displayRecord = ?";
$param = array($_SESSION["formid"], 'Y');
$stmt = sqlsrv_query($m_conn, $sql, $param);
$row = sqlsrv_fetch_array($stmt);
while ($row = sqlsrv_fetch_array($stmt)) {
$rowNum = $row['RowNumber'];
$firstN = $row['custFirt'];
$lastN = $row['custLast'];
}
When I try to include $rowNum, $firstN or $lastN in the $html such as
<td> '.$rowNum.'</td>
, it just shows up blank.
I'm not sure where the loop should go (which file) or how to include the $rowNum, $firstN and $lastN variables in the $html like the others.
I'm new to PHP (and relatively new to coding in general) and I don't have much experience working with it, but I've been able to make mPDF work for me in similar instances without the query included.
Any help would be greatly appreciated. Thank you so much!
I'm not sure how your loop interacts with the other two files, but this looks overly complex to me. I'd approach this in one .php file, something sort of like this:
<?php
//Include Files
include('mpdf/mpdf.php');
... //Your additional includes
//Define a row template string
$rowtemplate =<<<EOS
<tr>
<td>%%RowNumber%%</td>
<td>%%custFirt%%</td>
<td>%%custLast%%</td>
</tr>
EOS;
//Initialize the HTML for the document.
$html =<<<EOS
<h3> Form A </h3>
... //Your code
<td> Last Name: </td>
</tr>
EOS;
//Loop Code
$tbl = "form_Customers";
... //Your code
$row = sqlsrv_fetch_array($stmt);
while ($row = sqlsrv_fetch_array($stmt)) {
//Copy rowtemplate to a temporary variable
$out_tmp = $rowtemplate;
//Loop through your SQL variables and replace them when they appear in the template
foreach ($row as $key => $val) {
$out_tmp = str_ireplace('%%'.$key.'%%', $val, $out_tmp);
}
//Append the result to $html
$html .= $out_tmp;
}
// Close the open tags in $html
$html .= "</table></div>";
//Write the PDF
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output('cust-form-a', 'I');
I'm using heredoc syntax for the strings, since I think this is the cleanest way to include a large string.
Also, I prefer to omit the closing ?> tag as it introduces a stupid source of errors.

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