开发者

How to allow users to print and download MySQL query results

开发者 https://www.devze.com 2022-12-11 06:48 出处:网络
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 st

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 resul开发者_如何学运维t to a HTML page and printing the HTML page?


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)


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;  
?>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消