
Chris
Staff

Sep 9, 2009, 6:02 PM
Post #5 of 20
(5959 views)
Shortcut
|
|
Re: [sagentic] Save to .csv file
[In reply to]
|
Can't Post
|
|
Hi sagentic, Since this code is using getRecords() to load the records, any search functionality that works with a regular list viewer page will work with this one. In fact, you could use one PHP file to produce both your HTML-formatted results and CSV results. The following example would only work with a "get" request, so if your search form has method="post", you'll want to change that.
<?php require_once "C:/wamp/www/cmsbuilder_1_34_build1/cmsAdmin/lib/viewer_functions.php"; // load records list($blogRecords,) = getRecords(array( 'tableName' => 'blog', 'where' => "paid = '1'" )); // if the user has supplied "as_csv" in query string if (@$_REQUEST['as_csv']) { // specify fields to output $fields = array('title', 'category', 'content'); $filename = "blog_".date("Y-m-d_H-i",time()); header("Content-type: application/vnd.ms-excel"); header("Content-disposition: csv" . date("Y-m-d") . ".csv"); header( "Content-disposition: filename=".$filename.".csv"); // output csv header row print(join(',', $fields) . "\n"); // loop over records, outputting a row for each foreach ($blogRecords as $record) { $row = array(); foreach ($fields as $field) { $value = $record[$field]; // if this value contains a special character, quote and escape it if ( preg_match('/[," \t\n]/', $value) ) { $value = '"' . preg_replace('/"/', '""', $value) . '"'; } array_push($row, $value); } echo(join(',', $row) . "\n"); } // exit -- we are finished with the page exit; } ?> ... (a regular list page) ... <a href="?as_csv=1&<?php echo @$_SERVER['QUERY_STRING'] ?>">Download these results in CSV format</a> Chris
(This post was edited by chris on Sep 12, 2009, 8:19 AM)
|