Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php循环创建多个csv文件_Php_Loops_Csv - Fatal编程技术网

从php循环创建多个csv文件

从php循环创建多个csv文件,php,loops,csv,Php,Loops,Csv,我试图创建一个循环,当它执行时,它会创建多个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_q

我试图创建一个循环,当它执行时,它会创建多个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,但这对我来说不起作用。感谢您的帮助。
谢谢

不幸的是,您不能这样做,一个PHP请求会产生一个文件,并且没有办法解决这个问题。但是,您可以尝试将它们全部作为ZIP文件下载。看看f.e.

下面是一些解决方案,在某些情况下可能有用(在其他情况下可能很危险)风险自负

解决方法:按重定向循环
  • 正常输出单个文件
  • 重定向到步骤1中创建CSV文件的同一url,但在该url上附加GET标志,如
  • 确保在步骤#1中添加一个循环断路器,如
    if($i==10){exit;}
  • 解决方案B:按cronjob循环
  • 正常输出单个文件
  • 通过单独的cronjob调用处理第二个文件输出
  • 确保在步骤#1中添加一个循环断路器,如
    if($mycron==10){exit;}

  • 您不能通过for循环执行此操作

    但是,您可以创建一个php文件,它可以满足您的需要

     <a onclick="getcsv()" href="php_file_location.php?table_name=test"> Download </a>
    
     <script>
                                                
         function getcsv() {
                     window.open(php_file_location);
                 }
    </script>
    
    
    函数getcsv(){
    打开(php文件位置);
    }
    
    您是否已尝试更改每个项目的名称输出?像output1一样,output2?你是指文件名还是“$output”变量名?文件名=)我这样做了,但没有增加数字,而是在文件名上添加了每个部门的名称,但不幸的是,它只输出了一个随机部门名称的文件,如summary-a-Hull.csvOkay,所以请尝试创建类似的zip文件建议@Michael Kunst;)Zip文件看起来是个不错的选择。当您说1个请求=1个文件时。这是否意味着我必须发出多个请求才能获得我的身份,即多个页面加载(这不是一个好主意,因为浏览器可能会认为页面失败),或者是否可以通过ajax循环为每个部门请求数据。我知道这是一个不同的路径。每个部门的ajax循环肯定是一个选项。对于部分
    1 request=1 file
    请看这个问题:我明白了。我将尝试一下ajax选项,看看会发生什么,然后尝试使用zip文件。谢谢你的帮助:D