我制作了这个 PHP 脚本,它应该采用一个数组,并为数组中的每个元素生成一个 csv 文件。不幸的是,出了点问题。它不存储指定目录中的任何文件。但它也不会返回任何错误。也许有人可以看到问题?
$ids = json_decode($_POST['jsonarray']); // array sent with ajax
$start = $_POST['start']; // date sent with ajax
$end = $_POST['end']; // date sent with ajax
$start_date = date('yyyy-mm-dd', strtotime($start)); // format dates to sql firendly
$end_date = date('yyyy-mm-dd', strtotime($end));
$toZip = array(); // Prepare array to files for zip
if(is_array($ids)) {
foreach ($ids as $key => $qr)
{
// Get labels first
// Here we prepare the first line in the .CSV file
$tb = $qr . '_labels';
$sql = $user_pdo->query("SELECT * FROM $tb");
$head_array = array('Log ID', 'Timestamp');
while ($row = $sql->fetch(PDO::FETCH_ASSOC))
{
// This array is the first line in the .CSV file
$head_array[] = $row['label'];
}
// Get ready for looping through the database
$table = $qr . '_data';
$results = $user_pdo->prepare("SELECT * FROM $table WHERE timestamp BETWEEN :start_date AND :end_date;");
$results->bindParam(':start_date', $start_date, PDO::PARAM_STR);
$results->bindParam(':end_date', $$end_date, PDO::PARAM_STR);
$results->execute();
// Pick a filename and destination directory for the file
$filename = "temp/db_user_export_".time().".csv";
// Actually create the file
// The w+ parameter will wipe out and overwrite any existing file with the same name
$handle = fopen($filename, 'w+');
// Write the spreadsheet column titles / labels
fputcsv($handle, $head_array);
// Write all the user records to the spreadsheet
foreach($results as $row)
{
// amount of rows is unknown
$rows = $row->rowCount();
$insert_array = array();
for ($i=0; $i<=$rows; $i++)
{
// function goes here
$insert_array[] = $row[$i];
}
fputcsv($handle, $insert_array);
}
// Finish writing the file
fclose($handle);
$toZip[] = $filename;
}
}
示例var_dump($ids);
array(4) {
[0]=>
string(5) "t23ry"
[1]=>
string(5) "6us32"
[2]=>
string(5) "se43z"
[3]=>
string(5) "o00gq"
}