【问题标题】:Display results then give option to export to .csv?显示结果然后提供导出到 .csv 的选项?
【发布时间】:2013-08-21 08:28:40
【问题描述】:

我的网站上有一个使用 PHP 和 mysql 数据库生成的表。我想让表格显示下面的选项,以将结果导出到 .csv 文件。

我试图在底部设置一个按钮/链接来运行下面的 php 文件。 但是它只是将整个 HTML 代码导出到 .csv 文件中......有人可以帮我吗?

然后我运行查询 -->

$num_fields = $result->field_count;

$headers = array();

 // Creating headers for output files
for ($i = 0; $i < $num_fields; $i++)
{
    $headers[] = $result->fetch_fields();
}

$fp = fopen('php://output', 'w');
if ($fp && $result)
{
 // name of file with date
    $filename = "AccessReport-".date('Y-m-d').".csv";

        // Setting header types for csv file.
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename='.$filename);
        header('Pragma: no-cache');
        header('Expires: 0');
        fputcsv($fp, $headers);

        while ($row = $result->fetch_assoc())
        {
             fputcsv($fp, $row);
        }

 }
 $result->close();

【问题讨论】:

    标签: php mysql csv


    【解决方案1】:

    通常我在控制器中创建一个新操作来下载 CSV,例如

    public function csvAction() {
    
            $year = $this->_getParam('year');
            $month = $this->_getParam('month');
            $week = $this->_getParam('week');
    
            $arrCsvOutputCols = array();
            $title = '';
    
            if ($year && $month && $week) {
                $models = $this->db()->fetchAll("
                SELECT 
                    YEAR(`create_date`) AS `year`,
                    MONTH(`create_date`) AS `month`, 
                    WEEK(`create_date`) AS `week`, 
                    DAY(`create_date`) AS `day`,
                    SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
                    SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
                    SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
                    SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
                    SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
                    SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
                    SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
                    SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
                FROM 
                    dtb_member 
                GROUP BY `year`, `month`, `week`,`day` HAVING `year` = ? AND `month` = ? AND `week` = ?
                ORDER BY `day` ASC;"
                , array($year, $month, $week));
                array_push($arrCsvOutputCols,'year','month','week','day');
                $title = '"Year","Month","Week","Day",';
            } elseif ($year && $month) {
                $models = $this->db()->fetchAll("
                    SELECT 
                        YEAR(`create_date`) AS `year`, 
                        MONTH(`create_date`) AS `month`, 
                        WEEK(`create_date`) AS `week`, 
                        SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
                        SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
                        SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
                        SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
                        SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
                        SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
                        SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
                        SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
                    FROM 
                        dtb_member 
                    GROUP BY `year`, `month`, `week` HAVING `year` = ? AND `month` = ? 
                    ORDER BY `week` ASC;"
                , array($year, $month));
                array_push($arrCsvOutputCols,'year','month','week');
                $title = '"Year","Month","Week",';
            } elseif ($year) {
                $models = $blood = $this->db()->fetchAll("
                    SELECT 
                        YEAR(`create_date`) AS `year`, 
                        MONTH(`create_date`) AS `month`, 
                        SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
                        SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
                        SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
                        SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
                        SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
                        SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
                        SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
                        SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
                    FROM 
                        dtb_member 
                    GROUP BY `year`, `month` HAVING `year` = ? 
                    ORDER BY `month` ASC;"
                , $year);
                array_push($arrCsvOutputCols,'year','month');
                $title = '"Year","Month",';
            } else {
                $models = $this->db()->fetchAll("
                    SELECT 
                        YEAR(`create_date`) AS `year`, 
                        SUM(case WHEN `age` < 21 then 1 else 0 end) `a`,
                        SUM(case WHEN `age` >= 21 AND `age` < 31 then 1 else 0 end) `b`,
                        SUM(case WHEN `age` >= 31 AND `age` < 41 then 1 else 0 end) `c`,
                        SUM(case WHEN `age` >= 41 AND `age` < 51 then 1 else 0 end) `d`,
                        SUM(case WHEN `age` >= 51 AND `age` < 61 then 1 else 0 end) `e`,
                        SUM(case WHEN `age` >= 61 AND `age` < 71 then 1 else 0 end) `f`,
                        SUM(case WHEN `age` >= 71 AND `age` < 71 then 1 else 0 end) `g`,
                        SUM(case WHEN `age` > 80 then 1 else 0 end) `h`
                    FROM 
                        dtb_member 
                    GROUP BY `year` HAVING `year` > 0 
                    ORDER BY `year` ASC;"
                );
                array_push($arrCsvOutputCols,'year');
                $title = '"Year",';
            }
    
            ini_set('memory_limit','-1');
    
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender();
    
            header('Content-type: application/octet-stream');
            if (preg_match("/MSIE 8\.0/", $_SERVER['HTTP_USER_AGENT'])) {
                header('Content-Disposition: filename=age_' . time() . '.csv');
            } else {
                header('Content-Disposition: attachment; filename=age_' . time() . '.csv');
            }
            header('Pragma: public');
            header('Cache-control: public');
            array_push($arrCsvOutputCols,'a','b','c','d','e','f','g');
            $arrCsvOutputTitle = $title.'"0-20","21-30","31-40","41-50","51-60","61-70","71-80","81-"';
    
            echo mb_convert_encoding($arrCsvOutputTitle, 'SJIS-win', 'UTF-8') . "\r\n";
    
            foreach ($models as $model) {
                $item = $model;
                $cols = array();
    
                foreach ($arrCsvOutputCols as $col) {
                    if ($col) {
                        $value = $item[$col];
                        $value = str_replace("\r", "", $value);
                        $value = str_replace("\n", "", $value);
                        $value = str_replace("\"", "\"\"", $value);
    
                            array_push($cols, '"' . $value . '"');
    
                    } else {   
    
                    array_push($cols, '');
                    } 
                }
                echo mb_convert_encoding(join(",", $cols), 'SJIS-win', 'UTF-8') . "\r\n";
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2011-12-06
      • 2018-09-20
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多