从php循环创建多个csv文件

Ant*_*ent 6 php csv loops

我试图创建一个循环,在执行时它会创建多个 csv 文件并下载它们。这是我的代码:

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);
};
Run Code Online (Sandbox Code Playgroud)

目前,循环创建了 1 个带有新标题的 CSV 以及它下面的部门详细信息,如下所示在此处输入图片说明

我希望循环为每个部门创建一个新的 CSV,但它对我不起作用。任何帮助表示赞赏。谢谢

Mic*_*nst 3

不幸的是你不能,1 个 PHP 请求会生成一个文件,并且没有真正解决这个问题的方法。不过,您可以尝试将它们全部下载为 ZIP 文件。看看这个问题