close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Add Row to Recordset results to use in CSV Header?

Thread begun 2/01/2019 12:07 pm by Steve | Last modified 2/01/2019 2:43 pm by Ray Borduin | 571 views | 4 replies |

Steve

Add Row to Recordset results to use in CSV Header?

How can I add a row to a recordsets results to use as a CVS header?

I'm successfully writing a CSV file but there is no Header values. It's easy to generate these in PhpMyAdmin as an exported csv file.
The code below takes the results from $Recordset1 and puts them in an array ($DataArray). But it is overwriting the list of column names.

How can I manually push the row of column names into the 1st position of the array to use as CSV Header values?

php:
// output headers so that the file is downloaded rather than displayed

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


// output the column headings
$DataArray = array('invTitle','invDescr','Address','City','fid','key','FeatRoofType','FeatSidingType','FeatColor','FeatGarage','FeatCarport','FeatStorShed','FeatGutters','FeatShutters','FeatThermoWindw','FeatDeck','FeatPatio','FeatCeilings','FeatIntWalls','FeatHeating','FeatIntCAir','FeatIntWAir','FeatIntFirePl','FeatIntPantry','FeatIntSkyLight','FeatIntCFan','FeatIntCathC','FeatIntSunRoom','FeatIntGTub','FeatIntBasement','FeatIntWalkInCloset','FeatIntLaundry','FeatApplDishw','FeatApplDryer','FeatApplWasher','FeatApplGarbageD','FeatApplOven','FeatApplMicroW','FeatApplRefridge');


$DataArray = $Recordset1->Results; // puts results into array


// create a file pointer (FP) connected to the output stream
$fp = fopen('php://output', 'w');

foreach ($DataArray as $fields) {
    fputcsv($fp, $fields);
}


fclose($fp);
Sign in to reply to this post

Ray BorduinWebAssist

update this line:
$DataArray = $Recordset1->Results; // puts results into array

to:
$DataArray = array_merge($DataArray, $Recordset1->Results); // puts results into array

Sign in to reply to this post
Did this help? Tips are appreciated...

Steve

I tried array_unshift() also...

array_merge did not work :-(

php:
<?php

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=all-entries.csv');
// create a file pointer (FP) connected to the output stream
$fp fopen('php://output''w');


// output the column headings
$DataArray = array('invTitle','invDescr','Address','City','fid','key','FeatRoofType','FeatSidingType','FeatColor','FeatGarage','FeatCarport','FeatStorShed','FeatGutters','FeatShutters','FeatThermoWindw','FeatDeck','FeatPatio','FeatCeilings','FeatIntWalls','FeatHeating','FeatIntCAir','FeatIntWAir','FeatIntFirePl','FeatIntPantry','FeatIntSkyLight','FeatIntCFan','FeatIntCathC','FeatIntSunRoom','FeatIntGTub','FeatIntBasement','FeatIntWalkInCloset','FeatIntLaundry','FeatApplDishw','FeatApplDryer','FeatApplWasher','FeatApplGarbageD','FeatApplOven','FeatApplMicroW','FeatApplRefridge');


$DataArray array_merge($DataArray$Recordset1->Results); // puts results into array


foreach ($DataArray as $fields) {
    
fputcsv($fp$fields);
}


fclose($fp);
?>



Results are a csv file but no first row matching the manually constructed values.

Sign in to reply to this post

Ray BorduinWebAssist

How about you just write that row separately like:

php:
fputcsv($fp, $DataArray);


$DataArray = $Recordset1->Results; // puts results into array

foreach ($DataArray as $fields) {
    fputcsv($fp, $fields);
}
Sign in to reply to this post
Did this help? Tips are appreciated...

Steve

Yup, that works perfectly. Gracias Ray!

Code in case anyone else needs this:

php:
<?php

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=all-entries.csv');
// create a file pointer (FP) connected to the output stream
$fp fopen('php://output''w');


// output the column headings as array
$DataArray = array('invTitle','invDescr','Address','City','fid','key','FeatRoofType','FeatSidingType','FeatColor','FeatGarage','FeatCarport','FeatStorShed','FeatGutters','FeatShutters','FeatThermoWindw','FeatDeck','FeatPatio','FeatCeilings','FeatIntWalls','FeatHeating','FeatIntCAir','FeatIntWAir','FeatIntFirePl','FeatIntPantry','FeatIntSkyLight','FeatIntCFan','FeatIntCathC','FeatIntSunRoom','FeatIntGTub','FeatIntBasement','FeatIntWalkInCloset','FeatIntLaundry','FeatApplDishw','FeatApplDryer','FeatApplWasher','FeatApplGarbageD','FeatApplOven','FeatApplMicroW','FeatApplRefridge');

fputcsv($fp$DataArray); // put initial array into csv

$DataArray $Recordset1->Results// puts recordset results into existing array


foreach ($DataArray as $fields) {
    
fputcsv($fp$fields);
}


fclose($fp);
?>
Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...