2013-08-21 32 views
0

我在我的網站上有一個使用PHP和mysql數據庫生成的表格。我想用下面的選項顯示錶格以將結果導出到.csv文件。顯示結果然後給出選項導出到.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(); 

回答

0

通常我創建一個控制器中的新動作下載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"; 
     } 

    }