<?php

include("conexion2.php");
//include("csvwriter.php");
$str_sql = $_SESSION['sql_nomina'];
$rstSelect = $mysqli->query($str_sql);
$csv = new CSV(array('GUIA', 'ORDEN', 'DIRECCION', 'DESTINATARIO','DIRECCION','CODZONA',$Tespecial5,'TELEFONO','CIUDAD','ESPECIAL1','ESPECIAL2','ESPECIAL3','ESPECIAL4','ESPECIAL5','ESPECIAL6','ESPECIAL7','ESPECIAL8','ESPECIAL9','ESPECIAL10'));
while ($fila = $rstSelect->fetch_assoc()) {

    $csv->addRow(array($fila['guia'], $fila['orden'], $fila['direccion'], $fila['destinatario'],$fila['direccion'],$fila['cod_zona'],$fila['especial5'],$fila['telefono'],$fila['ciudad'],
            $fila['especial1'],$fila['especial2'],$fila['especial3'],$fila['especial4'],$fila['especial6'],$fila['especial7'],
            $fila['especial8'],$fila['especial9'],$fila['especial10']));
}
$filename = $_SESSION['nom_xls'];
$csv->export($filename);


class CSV {

    protected $data;

    /*
     * @params array $columns
     * @returns void
     */

    public function __construct($columns) {
        $this->data = '' . implode(';', $columns) . '' . "\n";
    }

    /*
     * @params array $row
     * @returns void
     */

    public function addRow($row) {
        $this->data .= '' . implode(';', $row) . '' . "\n";
    }

    /*
     * @returns void
     */

    public function export($filename) {
        header('Content-type: application/csv');
        header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
        echo $this->data;
        die();
    }
    public function __toString() {
        return $this->data;
    }

}
?>
