<?php
$finish_timeb = time();		
$texto_finishb= date("d/m/y", $finish_timeb);
require_once 'PHPExcel/Classes/PHPExcel.php';
	
$dbhost = 'localhost';  
$dbuser = 'trafika_bkpromo';  
$dbpass = "bkpromo@2017";  
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');  
$dbname = "trafika_bkpromofinal";
$connection = mysql_select_db($dbname); 
  
mysql_query("SET NAMES 'utf8'");
//print first employee table

$queryna = "SELECT * FROM facebook_oauth WHERE UID='".$_GET['id']."'";
			$resultna = mysql_query($queryna);
			$rowna=mysql_fetch_array($resultna);
			
$name = str_replace(' ', '_', $rowna['nombre']);			
$filename = 'Reporte_Tickets_'.$name.'_'.$texto_finishb; //your file name
			

$sql = "SELECT * FROM tickets WHERE activo = 1 AND id_participante= ".$_GET['id']." ORDER BY id_ticket DESC";
$rs_result = mysql_query ($sql);

$objPHPExcel = new PHPExcel();
/*********************Add column headings START**********************/
		$objPHPExcel->setActiveSheetIndex(0) 
					->setCellValue('A1', 'ID PARTICIPANTE')
					->setCellValue('B1', 'NO. TICKET')
					->setCellValue('C1', '4 DÍGITOS VISA')
					->setCellValue('D1', 'FECHA DE COMPRA')
					->setCellValue('E1', 'MONTO')
					->setCellValue('F1', 'FECHA DE REGISTRO');
		/*********************Add column headings END**********************/
		
		
$row = 2;
while($subrow = mysql_fetch_assoc($rs_result))
{
	
	
    $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['id_participante']);
    $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $subrow['ticket']);
    $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $subrow['producto']);
    $objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $subrow['fecha']);
    $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, '$'.$subrow['monto']);  
	$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $subrow['fecharegistro']);

    $row++;
}
/*********************Autoresize column width depending upon contents START**********************/
        foreach(range('A','F') as $columnID) {
			$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
		}
		/*********************Autoresize column width depending upon contents END***********************/
		
		$objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getFont()->setBold(true); //Make heading font bold
		
		/*********************Add color to heading START**********************/
		$styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'F0F0F0'),
        'size'  => 10,
        'name'  => 'Century Gothic',

    ),
	'fill'  => array(
    'type' => PHPExcel_Style_Fill::FILL_SOLID,
    'color' => array('rgb' => 'E77B00')
	),
	'alignment' => array(
            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
        )
	
	);
		$objPHPExcel->getActiveSheet()
					->getStyle('A1:F1')->applyFromArray($styleArray);
		/*********************Add color to heading END***********************/
		
		$objPHPExcel->getActiveSheet()->setTitle('Tickets Registrados'); //give title to sheet
		$objPHPExcel->setActiveSheetIndex(0);
		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;Filename=$filename.xls");
		header('Cache-Control: max-age=0');
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
		exit;
?>