Page 1 of 1

exporting csv file from php generated table

Posted: Tue Dec 14, 2010 11:16 am
by dilby
Hi all - I have a html table generated from php and am using a script I found on the net to export it to csv. The problem is, the exported csv file stops at the 2nd column and never
starts a new row. Is any one please able to help me shed some light as to what's going wrong?

Thanks

Code: Select all

<table class="statistics">
<thead>
<tr><th>
Name
</th><th>
Email
</th><th>
Date
</th></tr>
</thead>
<tbody>
<?php if( $page['reportByDate'] ) { ?>
	<?php foreach( $page['reportByDate'] as $row ) { ?>
	<tr>
		<td><?php echo $row['name'] ?></td>
		<td><?php echo $row['email'] ?></td>
		<td><?php echo $row['date'] ?></td>
	</tr>
	<?php } ?>

<?php } else { ?>
<?php } ?>
</tbody>
</table>

<?php } ?>

<?

    $csv_hdr = "Name, Email, Date";
    $csv_output .= $row['name'] . ", ";
    $csv_output .= $row['email'] . ", ";
    $csv_output .= $row['date'] . ", ";
 
    $csv_output .= $row['value'] . "\n"; //ensure the last column entry starts a new line ?>
 
<?

?>
<br />
<center>
<form name="export" action="export.php" method="post">
    <input type="submit" value="Save as CSV">
    <input type="hidden" value="<? echo $csv_hdr; ?>" name="csv_hdr">
    <input type="hidden" value="<? echo $csv_output; ?>" name="csv_output">
</form>
</center>

Code: Select all

<?php
/*
 This file will generate our CSV table. There is nothing to display on this page, it is simply used
 to generate our CSV file and then exit. That way we won't be re-directed after pressing the export
 to CSV button on the previous page.
*/

//First we'll generate an output variable called out. It'll have all of our text for the CSV file.
$out = '';

//Next we'll check to see if our variables posted and if they did we'll simply append them to out.
if (isset($_POST['csv_hdr'])) {
$out .= $_POST['csv_hdr'];
$out .= "\n";
}

if (isset($_POST['csv_output'])) {
$out .= $_POST['csv_output'];
}

//Now we're ready to create a file. This method generates a filename based on the current date & time.
$filename = $file."_".date("Y-m-d_H-i",time());

//Generate the CSV file header
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");

//Print the contents of out to the generated file.
print $out;

//Exit the script
exit;
?>

Re: exporting csv file from php generated table

Posted: Tue Dec 14, 2010 11:39 am
by Celauran

Code: Select all

<?php
/*
 This file will generate our CSV table. There is nothing to display on this page, it is simply used
 to generate our CSV file and then exit. That way we won't be re-directed after pressing the export
 to CSV button on the previous page.
*/

//First we'll generate an output variable called out. It'll have all of our text for the CSV file.
$out = '';

//Next we'll check to see if our variables posted and if they did we'll simply append them to out.
if (isset($_POST['csv_hdr'])) {
$out .= $_POST['csv_hdr'];
$out .= "\n";
}

if (isset($_POST['csv_output'])) {
$out .= $_POST['csv_output'];
}

//Now we're ready to create a file. This method generates a filename based on the current date & time.
$filename = $file."_".date("Y-m-d_H-i",time());

$fh = fopen($filename, 'w+');
fwrite($fh, $out);
fclose($fh);

//Exit the script
exit;
?>

Re: exporting csv file from php generated table

Posted: Wed Dec 15, 2010 4:21 am
by dilby
Thanks for the help, but export.php just white screens now. Any ideas?

Re: exporting csv file from php generated table

Posted: Wed Dec 15, 2010 10:21 am
by Celauran
If no file was created, it's most likely a permissions issue. Check that the webserver user has permission to write to that directory, or try creating the file elsewhere.