<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");
header('Content-type: text/html; charset=utf-8');
if(isset($_GET['userId']))
{
	$userId = (int)$_GET['userId'];
	$_SESSION['getUserCat']=$userId;
}
$zip = new ZipArchive;
$resultSelectNum2 = mysqli_query($conn, 'SELECT `id`,`name` FROM `user_category` WHERE `userId`="'.$_SESSION['getUserCat'].'"');
if ($zip->open('file/file.zip') === TRUE) 
{
	for ($i = 1;$i<$zip->numFiles; $i++) 
	{
		$filename = $zip->getNameIndex($i);
		$zip->deleteName($filename);
	
	}
	$zip->close();
}
$arrayFileName = array();
$counter = 0;
while($rowSelectNum2 = mysqli_fetch_assoc($resultSelectNum2))
{
	$counter++;
	$nameFile = $rowSelectNum2['name'];
	$arrayFileName[]="file/".$counter.".csv";
	$output = fopen("file/".$counter.".csv", 'w');
	$resultSelectNum = mysqli_query($conn, 'SELECT `user_category`.`name`,`user_number`.`number` FROM `user_number`,`user_category` WHERE `user_number`.`categoryId`="'.$rowSelectNum2['id'].'" AND `user_category`.`id` = `user_number`.`categoryId`');
	while($row = mysqli_fetch_assoc($resultSelectNum))
	fputcsv($output, $row);
	if ($zip->open('file/file.zip') === TRUE) 
	{
		$zip->addFile("file/".$counter.".csv", "".$counter.".csv");
		$zip->close();
	}
	fclose($output);

}
foreach($arrayFileName as $key=>$val)
	unlink($val);
header("location: file/file.zip",true);
exit;
ob_end_flush();  
?>
