加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从php循环创建多个csv文件

发布时间:2020-12-13 16:58:00 所属栏目:PHP教程 来源:网络整理
导读:我试图创建一个循环,在执行时它创建了多个csv文件并下载它们.这是我的代码: session_start();require '../connect.php'; //connect.php has connection info for my database// and uses the variable $connect$sqldept = "SELECT department_name from dep
我试图创建一个循环,在执行时它创建了多个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);
};

目前,循环创建了一个带有新标题的CSV,其下面的部门详细信息就像this

enter image description here

一样

我希望循环为每个部门创建一个新的CSV,但它不适合我.任何帮助表示赞赏.
谢谢

解决方法

不幸的是你不能,1 PHP请求导致一个文件,并没有真正解决这个问题.但是,您可以尝试将它们全部下载为ZIP文件.看看 this question f.e.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读