Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Download to CSV
Download to CSV
HI guys,
I would like to add an additional button next to the Print button with which I can import the Browse table into a CSV file. Has anyone done this before?
I would like to add an additional button next to the Print button with which I can import the Browse table into a CSV file. Has anyone done this before?
Re: Download to CSV
Thanks for you reply! That would be the manual way. But I want the date fields, integer fields formatted the way I set them. The print function does not take into account the format I have set. Since I have a lot of columns, formatting in Excel would be too time-consuming.
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Download to CSV
You can add a Procedure that runs some PHP code and saves data to a file. I'm going to post some code later
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Download to CSV
Here you go:
1. Create a Procedure: Builders -> Procedure -> Add
2. Code: BrowseDownloadToCSV
3. Give it a Description
4. Paste this PHP
(I could also have used nuBuilder's db connection and PHP functions. But in this way the code can also be used independently from nuBuilder)
5. Save
6. In you form's Custom Code, paste this JS:
7. Save
Now you see a new button in your Browse Screen (Download to CSV)!
1. Create a Procedure: Builders -> Procedure -> Add
2. Code: BrowseDownloadToCSV
3. Give it a Description
4. Paste this PHP
(I could also have used nuBuilder's db connection and PHP functions. But in this way the code can also be used independently from nuBuilder)
Code: Select all
// Use nuBuilder config file for db settings
include("nuconfig.php");
// Set the CSV delimiter
$delimiter = ';';
// Default file name if no has cookie (browse_export_filename) passed
$default_file_name = 'browse-export.csv';
// Setup the filename that our CSV will have when it is downloaded.
$fileName = "#browse_export_filename#";
if (strpos($fileName, '#') === 0) {
$fileName = $default_file_name;
}
//Connect to the DB using PDO.
try {
$pdo = new PDO("mysql:host=$nuConfigDBHost;dbname=$nuConfigDBName", $nuConfigDBUser, $nuConfigDBPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
echo "Error!: " . $e->getMessage() . "<br/>";
die();
}
//Retrieve Browse SQL from hash cookie
$sql = "#browse_sql#";
// Test with: $sql = "SELECT * FROM `zzzzsys_user` LIMIT 20";
//Prepare the SQL query.
$statement = $pdo->prepare($sql);
//Execute the SQL query.
$statement->execute();
//Fetch all of the rows from our table
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
//Get the column names.
$columnNames = array();
if(!empty($rows)){
//We only need to loop through the first row of our result
//in order to collate the column names.
$firstRow = $rows[0];
foreach($firstRow as $colName => $val){
$columnNames[] = $colName;
}
}
//Set the Content-Type and Content-Disposition headers to force the download.
header('Content-Type: application/excel');
header('Content-Encoding: UTF-8');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
//Open up a file pointer
$fp = fopen('php://output', 'w');
//Start off by writing the column names to the file.
fputcsv($fp, $columnNames, $delimiter);
//Then, loop through the rows and write them to the CSV file.
foreach ($rows as $row) {
fputcsv($fp, $row, $delimiter);
}
//Close the file pointer.
fclose($fp);
6. In you form's Custom Code, paste this JS:
Code: Select all
if(nuFormType() == 'browse'){
nuAddActionButton('nuRunPHPHidden', 'Download to CSV', 'nuSetProperty("browse_sql",nuCurrentProperties().browse_sql); nuRunPHP("DownloadCSV")');
}
Now you see a new button in your Browse Screen (Download to CSV)!
Last edited by kev1n on Tue Aug 25, 2020 1:44 pm, edited 2 times in total.
Re: Download to CSV
Fantastic, this works great!
Another wish: How can I only display the button if the print permission is set and otherwise do not allow the download?
Another wish: How can I only display the button if the print permission is set and otherwise do not allow the download?
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Download to CSV
Only add the "Download to CSV" button if the Print button exists:
Code: Select all
if(nuFormType() == 'browse'){
if ($('#nuPrintButton').length == 1) {
nuAddActionButton('nuRunPHPHidden', 'Download to CSV', 'nuSetProperty("browse_sql",nuCurrentProperties().browse_sql); nuRunPHP("DownloadCSV")');
}
}
-
- Posts: 45
- Joined: Mon Sep 02, 2019 11:54 am
Re: Download to CSV
Hi guys !
I tried the code whitout any changes and I don't know why but I have a html code in my CSV file. Is it normal ?
Thanks !
I tried the code whitout any changes and I don't know why but I have a html code in my CSV file. Is it normal ?

Thanks !
You do not have the required permissions to view the files attached to this post.