dilby Posted December 14, 2010 Share Posted December 14, 2010 Hi all - new here so please be gentle! 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, plus has an empty column at the end. Obviously i want several rows (depending on the mysql query used to populate the table, it could be tens of rows long.) Is any one please able to help me shed some light as to what's going wrong? Thanks <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> <?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; ?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted December 14, 2010 Share Posted December 14, 2010 Any quotes or anything in your csv_output field? That's kind of a strange way to do it, could you not just have a separate script that creates the CSV instead of storing the CSV in the form? If the table is huge you're passing a lot of data back and forth unnecessarily. Quote Link to comment Share on other sites More sharing options...
dilby Posted December 15, 2010 Author Share Posted December 15, 2010 HI Jon. To be honest I think I've bitten off more than I can chew here - I thought sometihng would be easily available bu I can't get anything to work. Are you please able to show me hwo you'd do it? Quote Link to comment Share on other sites More sharing options...
jdavidbakr Posted December 15, 2010 Share Posted December 15, 2010 Here's my CSV code, it's from a larger class that I use to draw tables but it should give an idea. There's more going on here than you would need, because the class itself is pretty detailed and specific, but hopefully this will help you start in the right direction. function export_mailmerge($statement,$filename) { // Print the headers $data = array(); $final = ''; foreach($this->header as $header=>$values) { $data[] = '"'.$values['text'].'"'; } $final .= implode(",",$data)."\r\n"; $result = mysql_query($statement); $current = 0; $last = mysql_num_rows($result); while ($current < $last) { $data = array(); foreach($this->header as $header=>$values) { $data[] = '"'.$this->get_data($result, $values, $current, $header).'"'; } $final .= implode(",",$data)."\r\n"; $current ++; } // Send the csv file as an attachment header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=".$filename); header("Pragma: no-cache"); header("Expires: 0"); echo $final; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.