views:

93

answers:

3

I've developed an application that users can use to store lists of their favourite books, books they've read, books they'd like to read, etc., and it's all working fine, but it's just suddenly struck me that users may want to print off their list or download their list to use in another format such as a CSV file to import into a spreadsheet.

Obviously users could print the entire web page, but I'd like to give them the ability to just print their list, or to download it as say a CSV file for use in a spreadsheet.

What would be the easiest way to do this, apart from passing the query result to a HTML page and printing the HTML page?

+2  A: 

To output a csv you can simply format the output from your query as a csv file, then set the content-disposition header for teh output. This snippet is taken from this website.

 <?
 $csv_output = "column 1,column2"; 
 $csv_output .= "\n"; 
 $result = mysql_query("select * from table"); 

 while($row = mysql_fetch_array($result)) { 
   $csv_output .= "$row[col1],$row[col2]\n";
   } 

 header("Content-type: application/vnd.ms-excel");
 header("Content-disposition: csv" . date("Y-m-d") . ".xls");
 print $csv_output;
 exit;  
?>
Vincent Ramdhanie
+4  A: 

For printing, most users appreciate the simplicity of a "cleaned-up" html page showing only/mostly the data they are interested in. In this fashion they can stay within the web browser to get a quick print of what they need. As indicated by Tom, this can often be achieved with the CSS whereby the undesired elements in a printout can be hidden and other elements may be shown with a slightly different style (for example to introduce more constrast, assuming a B/W printer etc.).

For exporting the data, you'll need to introduce a link / button which the users can click to signify their desire to get the data exported [to a particular format]. Your application can then serve this under the proper MIME type (i.e. you'll modify the http header 's Content-type (to be something like "Application/text") before streaming the CSV text)

mjv
+1  A: 

Obviously users could print the entire web page, but I'd like to give them the ability to just print their list, or to download it as say a CSV file for use in a spreadsheet.

Why not provide a print stylesheet, simple things like hiding the nav and menu and increase the font size.

Tom