Exporting PHP Table to CSV file

Hi,
I have a form that a user can submit, and it displays the results of their submission in a table on a new page. It also exports the data to a .csv file. All of the data being exported was originally part of a MySQL data base, but recently, I added in a new text field for Attention Line that a user just enters in on the form. Now my method of exporting the data as a CSV doesn’t pull the new text field (because it’s not stored in the database). Can anyone help with this? I’m new to programming, and am not sure the best way to revise my code. Here is what I have:

html form:

Mailing List Report

Please select the regions to be included:

New Hampshire

Vermont

Maine

Western Massachusetts

Rhode Island

Southern Maine

Northern Connecticut

Western Connecticut

Eastern New York Albany

Eastern New York Plattsburg

Central New York


Other: 

<label for “attnline”>Attention Line:


PHP Document:

<?php $astates = $_POST['state']; $attnline = $_POST['attnline']; print_r($astates); $link = mysqli_connect('localhost', 'uname', 'password', 'mailings'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $result = mysqli_query($link,"SELECT * FROM mailing_lists WHERE label IN ('".implode("','",$astates)."')"); echo ""; while($row = mysqli_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Attention Line First Name Last Name Hospital Name Address Line 1 Address Line 2 City State Zip Country
" . $attnline . "" . $row['first'] . "" . $row['last'] . "" . $row['hospital'] . "" . $row['addline1'] . "" . $row['addline2'] . "" . $row['city'] . "" . $row['state'] . "" . $row['zip'] . "" . $row['country'] . "
"; $fp = fopen('file.csv', 'w'); foreach ($result as $fields) { fputcsv($fp, $fields); } mysqli_close($link); ?>

[php]$fp = fopen(‘file.csv’, ‘w’);

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

if(fwrite($fp, $attnline) === FALSE) {
echo ‘Ah oh!’;
}

fclose($fp);[/php]

Should write one additional line to the file without it being in an array

Thanks so much for the reply lothop. This is really close to what I am trying to do. Right now, thanks to the code you provided, I am able to export the .csv and it has a new row at the bottom for the attention line. Is there a way to make it so that the $attnline shows up in a new column, instead of a row, and is displayed for each of the rows in my $results data? Thank you again for the help!

[php]while($row = mysqli_fetch_array($result)) {
echo "


" . $attnline . " " . $row['first'] . " " . $row['last'] . " " . $row['hospital'] . " " . $row['addline1'] . " " . $row['addline2'] . " " . $row['city'] . " " . $row['state'] . " " . $row['zip'] . " " . $row['country'] . " "; }[/php]

Just alter your echo section to have it display how you want it to.
Is this what you mean? or in your file output code?

Sorry, I meant in the .csv file that is exported. Take a look at the file I attached. I think that will show better what I am hoping to do.

Thanks again for any help/suggestions. It’s hard being a newbie!


I figured it out! After I echoed my table I just added this:

$fp = fopen(‘file.csv’, ‘w’);
$attnline = $attnline . “,”;
foreach ($result as $fields) {

if(fwrite($fp, $attnline) === FALSE) {
echo ‘Ah oh!’;
}
fputcsv($fp, $fields);
}

fclose($fp);

mysqli_close($link);

Thanks for your help earlier!

Well done mate :slight_smile: Glad to see you using your initiative and solving your own issues!

All the best

Sponsor our Newsletter | Privacy Policy | Terms of Service