Last active
April 9, 2021 14:45
-
-
Save dario61081/646331b04ba9de8f8b8342edc5379e38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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....)