<?php
ini_set("memory_limit","30000M");
set_time_limit(0);
include('../../../configuracion.php');

$filtro = "";


$matriz = array();
$cabeceras = array(
    "A" => "REGION",
    "B" => "NOMBRE",
    "C" => "CODIGO",
    "D" => "ZONA",
    "E" => "RANGO 0 a 3.5",
    "F" => "RANGO 3.5 a 6",
    "G" => "RANGO 6.1 a 10",
    "H" => "RANGO 10.1 a 20",
    "I" => "RANGO 20.1 a 50",
    );


header('Content-Type: application/octet-stream;charset=UTF-8"');
header('Content-Length: ' . filesize($data));
header("Content-disposition: attachment; filename=\"despachos.csv\"");
//preparar el wrapper de salida
$fp = fopen('php://output', 'w');
        

//$fp = fopen('archivonuevo.csv', 'w');
fputcsv($fp, $cabeceras, ";");

$total_reg = $db->getVar("comunas","COUNT(id)","id!='0' ".$filtro." ");


  


$i=0;
$sumatoria = 1000;

//echo $total_reg."<br>";


while($i<$total_reg){



  
$limite = $i.",".$sumatoria;

//echo $limite."***LIMITE*****<br>";
//$resultados = $db->get_results("Select * FROM socios WHERE id!='0' ".$filtro." order by id asc","ARRAY_N");

$resultados = $db->results("comunas","*","id!='0' ".$filtro." ","id asc",$limite);

foreach($resultados as $dato){

$i++;

$region = $db->getvar("regiones","nombre","id='{$dato->id_region}'");

$region = utf8_decode($region);
$nombre = utf8_decode($dato->nombre);

$v = array(str_replace("&#39;","'",$region),$nombre,$dato->codigo,$dato->zona,$dato->rango_1,$dato->rango_2,$dato->rango_3,$dato->rango_4,$dato->rango_5);

fputcsv($fp, $v, ";");

//echo $i."<br>";

if($i==$total_reg){ break;}



}
$sumatoria += $i;

}
fclose($fp);
exit;