Export Data to CSV (by Felice Di Stefano)
(for PHP MySQL, PHP ADODB, ASP Vbs, ASP Js)

This server behavior will apply a block of code which makes possible to create a CSV file from a user defined recordset and this can be saved and downloaded. The file may have the .csv or .txt extension and will assemble just the result of the query previously created by the user and will include column headings applying appropriate qualifiers automatically using the chosen sign.

Applying

The behavior will have to be applied on a transitory page free of html code to make possible the download. Therefore create a new page and open it, create the recordset you require to be exported as CSV, the page may have a restriction access code if you wish, as for example the Restrict Access provided by DW, open the Export Data to CSV server behavior from the Import-Export CSV submenu in the server behaviors palette, apply the parameters and click OK. Then go in code view and remove all the html code (from <html> to </html>) and save it.
You will need a page which will point, via link or via form, to the one we have now created. The code offer the possibility to work under condition or straight depending on how you configured it when applying it. For a download of a result straightly organized by you it could be enough a simple link as (<a href="thePageName">Download</a>). But you may want the code to run under the condition of matching a variable value of the type Get, Post or Session. Therefore you can use a link with appended Url parameters or a submit action to point to the page which runs the code or else.

Exp Recordset: Select here the recordset you created for export.
Delimiter: Define a delimiter which may be a char or a combination of chars, it may be also a single or more spaces. If you want use a tab as delimiter apply it as \t
Text Qualifier: Choose here the preferred qualifier which will be applied the same to all column headings but will be applied appropriately to the values depending on the recognized column definitions.
File Name: Define in this fields the name will have the file inserting the name without the extension and selecting the extension from the dropdown. Only "csv" and "txt" are available.
Trigger by: Select here the object that will trigger the code, if * Nothing is selected the related fields will be disabled and the code will execute with no condition. Otherwise the code will execute depending on the setting of the related fields.
Var Name: Insert the name if the variable you want the code to check, may be a Get, Post or Session variable depending on the Trigger selection.
With Value: Define the variable value that the will be matched in order to execute the export, if no match occurs the export will not be performed and a user defined error message will display.
The variable value can be also dynamic and you may use these forms:
PHP = " . $myvar . "
ASPvbs = " & myvar & "
ASPjs = " + myvar + "
Not Allowed Msg: Insert the error message to display in case of value not matched.
Allow Save/Download: Check the operation to perform, at least one has to be chosen but both together can be performed. These settings will enable/disable some related fields.
Where to Save: If save is checked you can assign here a path where to save the created files, the location must be created before with full permission. You may leave the field blank if you want to save at the same level of the script.
Empty Data Msg: Define in this field a message to display in case the recordset happen to be empty.
Redirect To: Define here a page to redirect to if the save operation was successful. It will be disabled if download is checked.

Note: The "fx_csvmsg" include file will be created in the FXInc folder, this contains the html output of the error message, you may customize the html output as you wish just don't damage the server code.


by FELIXONE