Im trying to create a loop that when executed it created multiple csv files and downloads them. This is my code:
session_start(); require '../connect.php'; //connect.php has connection info for my database // and uses the variable $connect $sqldept = "SELECT department_name from department;"; $departments = mysqli_query($connect, $sqldept); while ($department = mysqli_fetch_array($departments)) { $department = $department[0]; header('Content-Type: text/csv; charset=utf-8'); header("Content-Transfer-Encoding: UTF-8"); header('Content-Disposition: attachment; filename=summary-' . $department . '.csv'); header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1 header("Pragma: no-cache"); // HTTP 1.0 header("Expires: 0"); // Proxies $date = date("Y-m-d", strtotime("-28 days" . date("Y-m-d"))); $edate = date("Y-m-d"); $startdate = "(time.dateadded BETWEEN '$date' AND '$edate') AND"; $department = " and department_name = '$department'"; // create a file pointer connected to the output stream $output = fopen('php://output', 'w'); // output the column headings $sql2 = "SELECT time.id as timeid, time.staff_id, SUM(time.timein), COUNT(NULLIF(time.reasonforabsence,'')) AS count_reasonforabsence, GROUP_CONCAT(CONCAT(NULLIF(time.reasonforabsence,''),' ', date_format(time.dateadded, '%d-%m-%Y'),' ')) AS reasonforabsence, time.dateadded, staff.id AS staffid, department.id AS departmentid, department.department_name, staff.staff_name, staff.department_id, SUM(staff.workhoursperday), staff.payrollnum FROM time, staff, department WHERE $startdate staff.id = time.staff_id AND staff.department_id = department.id $department $staffsearch GROUP BY staff.id ORDER BY `time`.`dateadded` ASC;"; // output headers so that the file is downloaded rather than displayed fputcsv($output, array( 'Payroll Number', 'Name', 'Department', 'Hours Worked', 'Days Absent', 'Overtime', 'Reasons for Absence' )); $rows = mysqli_query($connect, $sql2); while ($rowcsv = mysqli_fetch_assoc($rows)) { $reasonforabsence = $rowcsv['reasonforabsence']; //$reasonforabsence = explode( ',', $rowcsv['reasonforabsence'] ); $overtime = 0; if (empty($rowcsv['SUM(time.timein)']) == true) { $rowcsv['SUM(time.timein)'] = 0; } ; if ($rowcsv['SUM(time.timein)'] > $rowcsv['SUM(staff.workhoursperday)']) { $overtime = $rowcsv['SUM(time.timein)'] - $rowcsv['SUM(staff.workhoursperday)']; } ; fputcsv($output, array( $rowcsv['payrollnum'], $rowcsv['staff_name'], $rowcsv['department_name'], $rowcsv['SUM(time.timein)'], $rowcsv['count_reasonforabsence'], $overtime, $reasonforabsence )); }; readfile("php://output"); fclose($output); };
Currently the loop created 1 CSV with a new header and the department details below it like this
I want the loop to create a new CSV for each department but its just not working for me. Any help is appreciated. Thanks
Advertisement
Answer
Unfortunately you can’t, 1 PHP Request results in one file, and there isn’t really a way around this. You can, however, try to download them all as a ZIP file. Take a look at this question f.e.