Reputation: 543
What I am doing is allowing my client to download a CSV file of all their orders in the database. That I can do, but what I can't seem to figure out if how to organise that file so that the information is clear and easy to understand in the file itself.
Here is my working code that creates the file for download:
$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);
The columns are being created with the headings listed above, but almost all the information is squeezed into the first column and the rest is just spread over the other columns. It looks a mess and wouldn't make much sense to anyone.
I was wondering if there was a way to force which row of data goes into each column. Thanks in advance.
Upvotes: 1
Views: 190
Reputation: 64536
By default, when Excel reads a CSV, it won't automatically expand all of the columns so the data can look as if there's something wrong with the CSV content itself, when actually you just need to configure Excel or expand them manually.
Upvotes: 1