2014-06-18 71 views
-2

我有以下腳本:PHP生成CSV文件僅適用第一次

<?php 
//Define basepath for codeigniter 
define('BASEPATH', '/'); 

//Include constants.php config file from codeigniter 
require_once "../../my_manager/system/application/config/constants.php"; 

//Check username and password from GET 
if($_GET['username'] != IMPORT_USERNAME || $_GET['password'] != IMPORT_PASSWORD) 
{ 
    header('HTTP/1.0 401 Unauthorized'); 
    echo "Denied Access"; 
    return; 
} 

//running scripts 
include 'export_table1.php'; 
include 'export_table2.php'; 
include 'export_table3.php'; 
include 'export_table4.php'; 
?> 

Eache出口是這樣的:

<?php 
//$host="localhost"; 
$host=""; 
$user=""; 
$pass=""; 
$db_name=""; 
$table="table1"; 
$conn = mysqli_connect($host,$user,$pass,$db_name) or die("Connection Error"); 

$query = "SELECT * FROM $table ORDER BY ID"; 
$result = mysqli_query($conn,$query) or die("sql error"); 
if(mysqli_num_rows($result)>0) 
{ 
$csv = ""; 

$row = mysqli_fetch_assoc($result); 

$delim = ""; 
//retrieving first line fields 
foreach($row as $k => $v) 
{ 
$csv .= $delim . '"' . str_replace('"', '""', $k) . '"'; 
$delim= ";"; 
} 
$csv .= "\n"; 

//retrieving value into fields 
while($row = mysqli_fetch_assoc($result)) 
{ 

$delim = ""; 
foreach($row as $v) 
{ 
$csv .= $delim . '"' . str_replace('"', '""', $v) . '"'; 
$delim = ";"; 
} 
$csv .= "\n"; 

} 
header("Content-type: text/csv"); 
header("Content-Disposition: attachment; filename=".$table.".csv"); 
echo $csv; 
exit 
} 
else 
{ 
echo "No records"; 
} 
exit 

?> 

的問題是,只有第一個腳本被激發,所以我只能將第一個表格下載到csv文件中。如果我首先啓動table2導出,則只會觸發table2導出。我該如何管理?提前致謝!

+0

因爲你在出口腳本的末尾退出...!另外,您不能在一個響應中輸出多個CSV文件,它將全部成爲一個文件。 – deceze

回答

2

據我所知「退出」殺死孩子和父母的腳本 - 這就是爲什麼你只能得到第一個表導出在CSV中的原因。繼續使用「返回」。