<?php
include("../script/php/konecta.php");

$name = 'Reporte';  
$nombreArchivo="simulacro-y-test";
$fechadescarga=date("YmdHis");

header("Content-Encoding: UTF-8");
header("Content-type: text/csv;charset=UTF-8");
header("Content-Disposition: attachment; filename=reporte-$nombreArchiv-$fechadescarga.csv");
header("Pragma: no-cache");
header("Expires: 0");

$sql = "SELECT * FROM alumnos";
if ($result = $nuevakonex->query($sql)) {
    //echo "\xEF\xBB\xBF";    
    $stream = fopen('php://output', 'w');
    // Poner titulos
        $row['camp0']=$field->camp0;
        $row['cod_al']=$field->cod_al;
        $row['nombre']=$field->nombre;
        $row['apelli1']=$field->apelli1;
        $row['telef1']=$field->telef1;
        $row['telef2']=$field->telef2;
        $row['email']=$field->email;
        $row['edad']=$field->edad;
        $row['sexo']=$field->sexo;
        $row['colegio']=$field->colegio;
        $row['ciudad']=$field->ciudad;
        $row['direccion']=$field->direccion;
        $row['avenida']=$field->avenida;
        $row['distrito']=$field->distrito;
        $row['apelli2']=$field->apelli2;
        $row['carrera1']=$field->carrera1;
        $row['carrera2']=$field->carrera2;
        $row['fecha']=$field->fecha;

        fputcsv($stream, array($row['camp0'],$row['cod_al'],$row['nombre'],$row['apelli1'],$row['telef1'],$row['telef2'],$row['email'],$row['edad'],$row['sexo'],$row['colegio'],$row['ciudad'],$row['direccion'],$row['avenida'],$row['distrito'],$row['apelli2'],$row['carrera1'],$row['carrera2'],$row['fecha']),';');

    while($field = $result->fetch_object()){
        // Llenar datos
        $row['camp0']=$field->camp0;
        $row['cod_al']=$field->cod_al;
        $row['nombre']=$field->nombre;
        $row['apelli1']=$field->apelli1;
        $row['telef1']=$field->telef1;
        $row['telef2']=$field->telef2;
        $row['email']=$field->email;
        $row['edad']=$field->edad;
        $row['sexo']=$field->sexo;
        $row['colegio']=$field->colegio;
        $row['ciudad']=$field->ciudad;
        $row['direccion']=$field->direccion;
        $row['avenida']=$field->avenida;
        $row['distrito']=$field->distrito;
        $row['apelli2']=$field->apelli2;
        $row['carrera1']=$field->carrera1;
        $row['carrera2']=$field->carrera2;
        $row['fecha']=$field->fecha;
        fputcsv($stream, array($row['camp0'],$row['cod_al'],$row['nombre'],$row['apelli1'],$row['telef1'],$row['telef2'],$row['email'],$row['edad'],$row['sexo'],$row['colegio'],$row['ciudad'],$row['direccion'],$row['avenida'],$row['distrito'],$row['apelli2'],$row['carrera1'],$row['carrera2'],$row['fecha']),';');
    }        
    fclose($stream);
}
$result->close();
?>