Skip to content

Instantly share code, notes, and snippets.

@dario61081
Last active April 9, 2021 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dario61081/646331b04ba9de8f8b8342edc5379e38 to your computer and use it in GitHub Desktop.
Save dario61081/646331b04ba9de8f8b8342edc5379e38 to your computer and use it in GitHub Desktop.
<?php
include_once implode(DIRECTORY_SEPARATOR, [__DIR__, 'conector.inc.php']);
const debug=true;
$format=$_GET['format']??'json';
if (debug){
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
$params = [];
/*buscandod formatear parametros $_GET*/
$datos = implode($_GET," and ");
$output = implode(', ', array_map(
function ($v, $k) {
if(is_array($v)){
return $k.'[]='.implode('&'.$k.'[]=', $v);
}else{
return $k.'='.$v;
}
},
$_GET,
array_keys($_GET)
));
foreach($_GET as $key=>$value){
if (!empty($value)){
if (strpos($value, ",")>0){
$valores = explode(",", $value);
$nuevos_valores = "";
foreach($valores as $k){
$nuevos_valores .= "'$k',";
}
$nuevos_valores = rtrim( $nuevos_valores,",");
array_push($params, implode("",array($key => "$key in ($nuevos_valores)")));
}else{
array_push($params, implode("", array($key=>"$key='$value'")));
}
}
}
$output = implode( " and ", $params);
$c = get_conector_pdo();
$q = 'select dg.* from datos_generales dg where ' . $output;
if (!empty($output)){
$r = $c->query($q);
}else{
$r = $c->query('select * from datos_generales');
}
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv;charset=UTF-8');
header('Content-Disposition: attachment; filename="export.csv"');
echo "\xEF\xBB\xBF";/*hacky ut8 boom*/
$data = $r->fetchAll(PDO::FETCH_ASSOC); //datatables require un item data con los valores de los campos
$header =true;
foreach($data as $row){
if ($header){
$h = array_keys($row);
echo implode(";", $h) . PHP_EOL;
$header = false;
}
echo implode(";", $row). PHP_EOL;
}
die();
<?php
include_once implode(DIRECTORY_SEPARATOR, [__DIR__, 'conector.inc.php']);
const debug=false;
if (debug){
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
$params = [];
/*buscandod formatear parametros $_GET*/
$datos = implode($_GET," and ");
$output = implode(', ', array_map(
function ($v, $k) {
if(is_array($v)){
return $k.'[]='.implode('&'.$k.'[]=', $v);
}else{
return $k.'='.$v;
}
},
$_GET,
array_keys($_GET)
));
foreach($_GET as $key=>$value){
if (!empty($value)){
if (strpos($value, ",")>0){
$valores = explode(",", $value);
$nuevos_valores = "";
foreach($valores as $k){
$nuevos_valores .= "'$k',";
}
$nuevos_valores = rtrim( $nuevos_valores,",");
array_push($params, implode("",array($key => "$key in ($nuevos_valores)")));
}else{
array_push($params, implode("", array($key=>"$key='$value'")));
}
}
}
$output = implode( " and ", $params);
$c = get_conector_pdo();
$q = 'select dg.* from datos_generales dg where ' . $output;
if (!empty($output)){
$r = $c->query($q);
}else{
$r = $c->query('select * from datos_generales');
}
/*exportar datos*/
header('Content-Type: application/json;charset=utf-8');
$data = array("data"=>$r->fetchAll(PDO::FETCH_ASSOC)); //datatables require un item data con los valores de los campos
echo json_encode($data,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
@dario61081
Copy link
Author

Obs: los argumentos de la request se procesan para formar el WHERE de la consulta, este detecta los multvalores de los select multiple y genera el in (x,y....)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment