Last active
February 25, 2016 14:52
-
-
Save kurozumi/06bf2080910da92e62d6 to your computer and use it in GitHub Desktop.
【CodeIgniter3】League/CSVを使ってデータベースのデータをCSVファイルでダウンロードする方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
use League\Csv\Writer; | |
class Welcome extends CI_Controller { | |
public function index() | |
{ | |
// 一時ファイル作成 | |
$csv = Writer::createFromFileObject(new SplTempFileObject()); | |
// カラム名を取得 | |
$fields = $this->db->list_fields("table_name"); | |
// 一時ファイルにカラム名を追加 | |
$csv->insertOne($fields); | |
// DBからデータ取得 | |
$query = $this->db->query("SELECT * FROM table_name"); | |
// 一時ファイルにデータを追加 | |
$csv->insertAll($query->result_array()); | |
// CSVファイルとして出力 | |
$csv->output('table_name.csv'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment