创建多个csv文件从php循环

f45qwnt8  于 2022-12-30  发布在  PHP
关注(0)|答案(4)|浏览(128)

我试图创建一个循环,当执行它创建多个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);
};

当前,循环创建了1个CSV,其中包含一个新的标题,其下包含部门详细信息,如

我想循环创建一个新的CSV为每个部门,但它只是不为我工作。任何帮助是赞赏。谢谢

gwo2fgha

gwo2fgha1#

很遗憾你不能,一个PHP请求只会产生一个文件,而且没有办法解决这个问题。不过,你可以试着把它们都下载成一个ZIP文件。

wribegjk

wribegjk2#

以下是一些变通方法,在某些情况下可能有用(在其他情况下可能危险)。使用风险自担!

变通方案A:通过重定向循环

1.正常输出单个文件
1.重定向到在步骤#1中创建CSV文件的同一个url,但在该url后面附加一个GET标志,如http://www.example.net/output_csv?i=1
1.确保在步骤1中添加一个循环断路器,如if($i==10) { exit; }

变通方案B:通过cronjob循环

1.正常输出单个文件
1.通过单独的cronjob调用处理第二个文件输出。
1.确保在步骤#1中添加一个循环断路器,如if($mycron==10) { exit; }

iyr7buue

iyr7buue3#

你不能用for循环来做这件事。
然而,你可以做一个php文件,可以做你的目的.

<a onclick="getcsv()" href="php_file_location.php?table_name=test"> Download </a>

 <script>
                                            
     function getcsv() {
                 window.open(php_file_location);
             }
</script>
ds97pgxw

ds97pgxw4#

我遇到了前面提到的同样的问题。但是在我的例子中,我没有尝试下载多个CSV,而是将其上传到sFTP服务器。在创建文件而不是使用

$output     = fopen('php://output', 'w');

我以前

$output     = fopen($path_and_name, 'w');

其中$路径和名称=指向sftp文件夹的$路径。'/'. $文件名;执行后,正确的文件被上传到那里各自的文件夹正确的方式,我希望它是。但是的,错误的文件也下载了相同的问题,如上面发送。
因此,如果您正在寻找上传文件在服务器上,它可以做到(即使他们都有相同的名称)。

相关问题