Fputcsv with custom headers

Asked
Active3 hr before
Viewed126 times

7 Answers

headerscustom
90%

Trying to convert the following working code to PDO, the problem I am running into are the custom headings set in the fputcsv($output,array). I really want to move away from all msql_ functions so I would greatly appreciate some help.,I have gotten this far, without getting it to work. The problem here is that I am not able to set custom column names in the CSV file and it also does not export to a CSV., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , 1 How do you know? You have changed so much and are outputting to a different medium it's hard to tell where the problem is. You should start with just switching to PDO and change the other logic when you have that working. – jeroen Jan 27 '14 at 15:20

Here's what I came up with, it appears to work just as the original one. If you have any concerns please let me know, it works as intended and I got rid of the mysql_ functions.

< ? php

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=collections.csv');

try {
   $conn = removed
} catch (PDOException $e) {
   echo $e - > getMessage();
}

$sql = "SELECT 
coll.title AS CollectionTitle,
   coll.inclusivedates AS InclusiveDates,
   coll.ID AS CollectionID,
   collloc.LocationID,
   collloc.Content,
   collloc.RangeValue,
   collloc.Section,
   collloc.Shelf,
   collloc.Extent,
   collloc.ExtentUnitID
FROM tblCollections_Collections coll
JOIN tblCollections_CollectionLocationIndex collloc
ON collloc.CollectionID = coll.id
ORDER BY coll.ID ";

$results = $conn - > query($sql);

// Pick a filename and destination directory for the file
// Remember that the folder where you want to write the file has to be writable
$filename = "collections.csv";

// Actually create the file
// The w+ parameter will wipe out and overwrite any existing file with the same name
$handle = fopen('php://output', 'w');

// Write the spreadsheet column titles / labels
fputcsv($handle, array('Title', 'InclusiveDates', 'CollectionID', 'LocationID', 'Content', 'RangeValue', 'Section', 'Shelf', 'Extent', 'ExtentUnitID'));

// Write all the user records to the spreadsheet
foreach($results as $row) {
   fputcsv($handle, array($row['CollectionTitle'], $row['InclusiveDates'], $row['CollectionID'], $row['LocationID'], $row['Content'], $row['RangeValue'], $row['Section'], $row['Shelf'], $row['Extent'], $row['ExtentUnitID']));
}

// Finish writing the file
fclose($handle);

?
>
88%

fputcsv — Format line as CSV and write to file pointer, The optional eol parameter sets a custom End of Line sequence. , The optional enclosure parameter sets the field enclosure (one single-byte character only). , The optional separator parameter sets the field delimiter (one single-byte character only).

aaa, bbb, ccc, dddd
123, 456, 789
   ""
"aaa"
"", ""
"bbb"
""
72%

// open the file "demosaved.csv" for writing
$file = fopen('demosaved.csv', 'w');

// save the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));

// Sample data. This can be fetched from mysql too
$data = array(
   array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
   array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
   array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
   array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
   array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
);

// save each row of the data
foreach($data as $row) {
   fputcsv($file, $row);
}

// Close the file
fclose($file);
65%

I am trying to generate CSV with custom headers like USRNAME , USER PROFILE , EMAIL . ,After generating the CSV I rewrite it . ,I am using the 2nd way for sure using query directly is the best way ever :),2nd way(by using the UNION ALL) like this:

This is the code I am using .

$path = getcwd() . '/uploads/data.csv' ;
$sql = "select username, CONCAT('<a href="" $profile',username,'"">',username,'</a>') as profile_url ,email from `engine4_user` LIMIT 5000 into outfile '$path' FIELDS TERMINATED BY ',' LINES TERMINATED BY '
' ";

But I don't know how to add custom header , Or a custom row before the DB values. Please help me to add those fields .

I wants a CSV that will out put like:
   USERNAME, PROFILE, EMAIL
Jonson1, http: //mysite/profile/jonson1 , [email protected]
   Jonson2, http: //mysite/profile/jonson2 , [email protected]
   Jonson3, http: //mysite/profile/jonson3 , [email protected]
   Jonson4, http: //mysite/profile/jonson4 , [email protected]

   Now I generate:
   Jonson1, http: //mysite/profile/jonson1 , [email protected]
   Jonson2, http: //mysite/profile/jonson2 , jonson.te[email protected]
   Jonson3, http: //mysite/profile/jonson3 , [email protected]
   Jonson4, http: //mysite/profile/jonson4 , [email protected]

   I need to add
USERNAME, PROFILE, EMAIL
load more v
75%

The fgetcsv reads a line from the provided file pointer and parses for CSV fields. It returns an array containing the fields read. The fputcsv takes an array of data and writes it as a CSV line to the specified file handle. , We have an array of users. We write the users into a CSV file with fputcsv. , We read a line with fgetcsv; the function returns an array of fields read. , The following example sends CSV data as an attachment to the user.

John, Doe, gardener
Lucy, Smith, teacher
Brian, Bethamy, programmer
load more v
40%

CSV data may contain an optional header line at the beginning in the same format as other rows. For example: column_name1,column_name2,column_name3 CRLF col_11,col_12,col_13 CRLF col_21,col_22,col_23 ,Read CSV using PHP built-in functions,The following code uses custom PHP function str_putcsv() to get the array data and put it into the file target as specified. This custom function uses PHP built-in fputcsv() on each array iteration.,As like as the above example, we are going to use PHP fgetcsv() function to read and process the CSV file data containing Comma in its value.

        col_11, col_12, col_13 CRLF
        col_21, col_22, col_23 CRLF
load more v
22%

Line 5 is to add the column headers. You can skip this if you don’t want any column headers,How to create a zip file using PHP ,If you want to store the data into a csv file, then you can use the code similar to the following,Great script, thanks. The only issue I get is that fputcsv add double quote to some data and I need to get rid of those chars. Any hint?

If you want to store the data into a csv file, then you can use the code similar to the following

// open the file "demosaved.csv" for writing
$file = fopen('demosaved.csv', 'w');

// save the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));

// Sample data. This can be fetched from mysql too
$data = array(
   array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
   array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
   array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
   array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
   array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
);

// save each row of the data
foreach($data as $row) {
   fputcsv($file, $row);
}

// Close the file
fclose($file);
load more v

Other "headers-custom" queries related to "Fputcsv with custom headers"