我正在做的是允许我的客户在数据库中下载他们所有订单的 CSV 文件。我可以做到,但我似乎无法弄清楚如何组织该文件,以便文件本身的信息清晰易懂。
这是我创建下载文件的工作代码:
$today = date("d-m-Y");
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=orders_' . $today . '.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('Product ID & Qty','First Name', 'Last Name', 'Total Spent', 'Payment Date'));
// fetch the data
include('../storescripts/connect_to_mysql.php');
$rows = mysql_query('SELECT product_id_array, first_name, last_name, mc_gross, payment_date FROM transactions ');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
列是使用上面列出的标题创建的,但几乎所有信息都被压缩到第一列,其余的只是散布在其他列上。它看起来一团糟,对任何人都没有多大意义。
我想知道是否有一种方法可以强制将哪一行数据放入每一列。提前致谢。