Jak jsi tam psal, ty zadas pozadavek a db ti vrati vysledky ala json, treba. Tak ti to muze vracet jako csv, ten samy php kod. Tak jsem to myslel. Prijde mi to jednodussi nez se drbat s javascriptem, kdyz uz mas vlastne vse hotove.
Pokud to ale nasledne jeste nejak filtrujes u uzivatele, i tak by bylo jednodussi poslat ty filtry php. Hlavne jde o to, ze php ma na csv funkce.
$filters = $_POST['filters']; // samozrejme bys pridal kontroly proti hackingu a tak
$q = "SELECT ... WHERE ".$filters;
$data = ... fetch();
// https://stackoverflow.com/…array-to-csv
function array2csv($data, $delimiter = ',', $enclosure = '"', $escape_char = "\\")
{
$f = fopen('php://memory', 'r+');
foreach ($data as $item) {
fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
}
rewind($f);
return stream_get_contents($f);
}
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
var_dump(array2csv($list));
// ---NEBO---
$f=fopen('php://memory','w');
$header=array("asdf ","asdf","asd","Calasdflee","Start Time","End Time" );
fputcsv($f,$header);
fputcsv($f,$header);
fputcsv($f,$header);
fseek($f,0);
header('content-type:text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
fpassthru($f);```
Tuhle jsem delal pro nejaky program, export csv, json, xml, html table, php array().
<?php
// --- class ExportData ---
class classExportData
{
private $vars;
function __construct()
{
$this->vars = array();
$this->vars['name'] = 'classExportData'; // evid3 form name / what, template / htx file prefix
// $this->config = array('debug' => true);
}
private function isDebug()
{
return isset($this->config['debug']) && $this->config['debug']===true ? true : false;
}
private function errorDebug($fn='')
{
return !$this->isDebug() ? '' : 'class'.$this->vars['name'].'::'.$fn.' ';
}
private function errorAdd($fn='', $text='')
{
global $REPORT;
$REPORT[] = $this->errorDebug($fn).$text; //$app->errAdd('', $this->errorDebug($fn).$text)
}
// function defination to convert array to xml
public function arrayToXml( &$data_xml, $data_list=array() )
{
foreach( $data_list as $key => $value )
{
if (trim($key)=='')
{continue;}
if (is_array($value))
{
if (is_numeric($key))
{
$key = "item".$key; //dealing with <0/>..<n/> issues
}
$new_child = $data_xml->addChild($key);
$this->arrayToXml($new_child, $value);
}
else {
if (is_numeric($key))// if the key is an integer, it needs text
{
$key = "key".$key;
}
$new_child = $data_xml->addChild("$key", htmlspecialchars("$value"));
}
}
}
public function csvStrToCsv($input=array(), $delimiter = ',', $enclosure = '"')
{
if (!is_array($input))
{return false;}
foreach($input as $key=>$value)
{
if (is_array($value))
{
$input[$key] = $this->csvStrToCsv($value, $delimiter, $enclosure);
}
}
// if (is_array($input))
// {
// }
$fp = fopen('php://temp', 'r+'); // Open a memory "file" for read/write...
fputcsv($fp, $input, $delimiter, $enclosure);
rewind($fp);
$data = fread($fp, 1048576); // limit 1MB per csv line
fclose($fp);
return rtrim($data, "\n");
}
public function format($data=array(), $format='') // output format php array (/ json / xml / csv - not implement)
{
if ($format=='')
{
return $data;
}
elseif ($format=='csv')
{
$content = '';
$valenc = "\"";
$coldel = ";";
$rowdel = "\n";
// $rowdel = "<br>"; // for html/text
foreach ($data as $row)
{
$content .= $this->csvStrToCsv($row, $coldel) . $rowdel; // excel csv, del=; enc=" row=\n
}
return $content;
}
elseif ($format=='json')
{
return json_encode($data);
}
elseif ($format=='xml')
{
$xml = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
$this->arrayToXml($xml, $data);
return $xml->asXML();
}
elseif ($format=='html')
{
//var_dump($data); die();
$pad_table = "\n";
$pad_tbody = "\n ";
$pad_tr = "\n ";
$pad_td = "\n ";
$out = "$pad_table<table>$pad_tbody<tbody>";
foreach ($data as $row)
{
//var_dump(is_array($row), $row);
//die();
if (!is_array($row))
{
$out .= "$pad_tr<tr>$pad_td<td>".((string) $row)."</td>$pad_tr</tr>";
continue;
}
$out .= "$pad_tr<tr>";
foreach ($row as $col)
{
$out .= "$pad_td<td>".((string) $col)."</td>";
}
$out .= "$pad_tr</tr>";
}
$out .= "$pad_tbody</tbody>$pad_table</table>";
return $out;
}
}
}