The following code creates and downloads a file CSV
:
<?php
require_once 'config.php';
$salida = fopen('saldos.csv', 'w');
if ($salida) {
# Los encabezados no son necesarios en este caso
# fputs($salida, "Documento, Obtenido, Acumulado, Nuevo".PHP_EOL);
$sql = "SELECT documento, obtenido, acumulado, nuevo FROM datos WHERE semana = 6 ";
$stmt = $dbcon->query($sql);
$stmt->execute();
if ($stmt->rowCount() > 0 ) {
while ($fila = $stmt->fetch(PDO::FETCH_ASSOC)) {
fputs($salida, implode($fila, ';').PHP_EOL);
}
}
fclose($salida);
} else {
echo "Lo sentimos! No se pudo crear el archivo";
}
ob_end_clean();
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=saldos.csv');
readfile("./saldos.csv");
?>
Whose output is the following:
1040356985;22;92;114
15654895;26;74;100
I need the output of the file to be as follows: _ to be loaded later in a different application
1040356985;6;22
1040356985;7;92
1040356985;8;114
15654895;6;26
15654895;7;74
15654895;8;100
Where 6=obtenido
, 7=acumulado
, 8=nuevo
.
What should I change in the code PHP
to give the file that structure CSV
?
I think from what I understood from the other answers this is what you are looking for, add each option in the loop, 6,7,8.