column format in archico csv

0

I am downloading a database which has state, municipality and parish fields and each of them has a unique code when making the query to the database I get the code as follows: code_status = 01, comunidad_code = 01 but when the csv is generated these codes are eliminated 0, when they are opened with excel or with calc.

this is the function that the csv generates

function generarCsv($mayor5){
   $lista = array();
   foreach ($mayor5 as $cp) {
        foreach ($cp as $titulo => $value) {
           array_push($lista, $titulo);
        }
    break;

   }

    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"Base de Datos.csv\""); 

    //preparar el wrapper de salida
    $outputBuffer = fopen("php://output", 'w');   

    //volcamos el contenido del array en formato csv
    fputcsv($outputBuffer,$lista, ';');

        foreach($mayor5 as $codigo_persona  => $data) {
            foreach ($data as $i => $valor) {
                $data[$i] = utf8_decode($valor);
            }               
            if($codigo_persona!=""){
                fputcsv($outputBuffer,$data, ';');    
            }                
        }
    //cerramos el wrapper
    fclose($outputBuffer);
    exit;
    $this->index();
}

This is the arrangement as it comes from the database the fields in bold are the ones that when generating the file

[fecha_registro] => 2018-03-13 14:39:23.51861
[codigo_origen] => 1
[origen] => Plan Vulnerabilidad
[codigo_persona] => PER0106
**[cedula_ubicacion] => 0101020010000003**
**[codigo_estado] => 01**
[estado] => DISTRITO CAPITAL
**[codigo_municipio] => 01**
[nombre_municipio] => LIBERTADOR
**[codigo_parroquia] => 02**
[nombre_parroquia] => ANTIMANO
**[codigo_sector] => 001**
[nombre_sector] => EL SIFON
[nombres_apellidos] => NOEMI AGUIRRE
[cedula] => 2
[fecha_nacimiento] => 1950-11-10
[edad] => 67
[meses] => 4
[dias] => 3
[codigo_genero] => 2
[embarazo] => 1
[cbi] => 200
[peso] => 54.3
[talla] => 1.55
[codigo_descripcion_situacion] => 1'

I use the php fputcsv function. How could I solve this case?

    
asked by Angel Gutierrez 22.03.2018 в 15:39
source

1 answer

0

I made some tests, and quotation marks the number plus put the sign equal to the number with leading zeros, it was for Excel to respect this format when opening or importing data from a CSV. In your case, it would do something like:

// así viene de la BD:
            [cedula_ubicacion] => 0101020010000003

// así lo guardas en csv:
            ="0101020010000003"

// o sea, si hablamos de PHP:
           $valor = '="0101020010000003"';
    
answered by 22.03.2018 в 16:25