<?php
include("conexion2.php");
$currentPage = $_SERVER["PHP_SELF"];
$maxRows_Rs = 100000;// este numero es el numero de resultados que quieren que se vean por pagina pueden poner algo asi: $maxRows_Rs = 6; para ver paginas con 6 resultados
$pageNum_Rs = 0;
if (isset($_GET['pageNum_Rs'])) {
	$pageNum_Rs = $_GET['pageNum_Rs'];
}
$startRow_Rs = $pageNum_Rs * $maxRows_Rs;
mysql_select_db($db, $conexion);
$est=1;
// Excel begin of file header
function xlsBOF()
{
	echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
	return;
}
// Excel end of file footer
function xlsEOF()
{
	echo pack("ss", 0x0A, 0x00);
	return;
}
// Function to write a Number (double) into Row, Col
function xlsWriteNumber($Row, $Col, $Value)
{
	echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
	echo pack("d", $Value);
	return;
}
// Function to write a label (text) into Row, Col
function xlsWriteLabel($Row, $Col, $Value )
{
	$L = strlen($Value);
	echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
	echo $Value;
	return;
}
// ----- end of function library -----

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=servicio_cliente.xls" );
header ("Content-Description: PHP/INTERBASE Generated Data" );
xlsBOF(); // begin Excel stream
$tabla=$_GET['tabla'];
$reg=$_GET['registro'];
$est=$_GET['estado'];
$dev=$_GET['dev'];
unset($add);
unset($titulos);
$titulos = $_SESSION['titulos'];

$campos_consulta=$_SESSION['campos_consulta'];
if($reg==1){
	$add=$_SESSION['add'];
}else if($reg==0){
	$add=$_SESSION['add2'];
}else{

}

if($reg==2){
	if($est=='T'){
		$add=$_SESSION['add'];
	}elseif($dev==0){
		$add=$_SESSION['add'];
		$add=$add." AND g.id_estado='$est'";
	}else{
		$add=$_SESSION['add2'];
		$add=$add." AND g.id_devolucion='$dev'";
	}
}
$rst = "SELECT ".$campos_consulta." FROM ".$tabla." ".$add;

$query_limit_Rs = sprintf("%s LIMIT %d, %d", $rst, $startRow_Rs, $maxRows_Rs);
$Rs = mysql_query($query_limit_Rs, $conexion) or die(mysql_error());
$num = mysql_num_rows($Rs);
//$row_Rs = mysql_fetch_assoc($Rs);
for($i=0; $i<=count($titulos)-1; $i++)
{
	xlsWriteLabel(0,$i,$titulos[$i]);
	$total_campos=$total_campos+1;
}
$i=1;
//xlsWriteLabel(0,0,$num);
//xlsWriteLabel(0,1,$campos_consulta." FROM ".$tabla." ".$add);
do {
	for($j=0; $j<=$total_campos-1; $j++){
		$variable=$row_Rs[$j];
		if($i==1){

		}else{
			xlsWriteLabel($i-1,$j,$variable);
		}
	}
	$i=$i+1;
} while ($row_Rs = mysql_fetch_array($Rs));
// write a number B1
xlsEOF(); // close the stream
?>
