php mysql export should create dropdown - php

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

Related

how to write html table in .txt file using 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 .= "---------------------------------------";

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.

code to expand excel column width in php while exporting

I am new to PHP and trying to export some DB data to Excel file as below. But in the output I want the column widths to be adjusted automatically to view the entire content. See the code I am using to export data. I am not using any third party libraries for export.
<?php
require_once("db.php");
$contents="email,time_in,v_id,time_out\n";
$user_query = mysql_query('SELECT * FROM table');
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['email'].",";
$contents.=$row['time_in'].",";
$contents.=$row['v_id'].",";
$contents.=$row['time_out']."\n";
}
// remove html and php tags etc.
$contents = strip_tags($contents);
//header to make force download the file
header("Content-Disposition: attachment; filename=Report".date('d-m-Y').".xls");
print $contents;
?>
Is there any way to format the output ?

Create XLS file from MySQL database using PHP script

I want to generate XLS files with a PHP script from a MySQL database. The big problem here is that when I open the new exported .xls file, I see the values OK (i.e. it is correctly formatted) but the colour of the field changed to white. However, I need the colour unchanged, as is by default in Excel.
Here is the PHP script that I use to extract data from my database.
<?php
include 'connect.php';
$result = mysql_query('SELECT * FROM projects2');
?>
<center><h1>Lista valorilor din tabela</h1>
<h2>Exporta lista Clienti</h2></center>
<?php
include_once 'tabel_clientipm.php';
?>
The PHP file used to generate the XLS file is:
<?php
include 'connect.php';
$result = mysql_query('SELECT * FROM projects2');
if (isset($_GET['exporta_lista_clienti'])) {
$filename = 'raportnou.xls';
header("Content-type: application/ms-excel");
header("Content-Disposition: attachment; filename=$filename");
include_once 'tabel_clientipm.php';
exit();
}
?>
I added the tabel_clientipm.php:
<center>
<table border="1">
<tr>
<th>surname</th>
<th>name</th>
<th>age</th>
</tr>
<?php
while ($client = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $client['surname'];?></td>
<td><?php echo $client['name'];?></td>
<td><?php echo $client['age'];?></td>
</tr>
<?php
}
?>
</table>
</center>
If you want to create real .xls file NOT csv or html hidden in .xls extension use PHPExcel or
It supports the following formats.
BIFF 8 (.xls) Excel 95 and above
Office Open XML (.xlsx) Excel 2007 and above
If PHPExcel is slow for you check these alternatives (provided by an author of PHPExcel. All of them are faster than PHPExcel
If you export as a csv, you can probably import the data as colorless.

Categories