Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Download to CSV

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Download to CSV

Unread post by Timo »

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?
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Download to CSV

Unread post by admin »

Timo,

You should be able to import the HTML made by the print button.

Steven
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: Download to CSV

Unread post by Timo »

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.
kev1n
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

Unread post by kev1n »

You can add a Procedure that runs some PHP code and saves data to a file. I'm going to post some code later
kev1n
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

Unread post by kev1n »

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)

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);
5. Save

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")'); 
}
 
7. Save

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.
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: Download to CSV

Unread post by Timo »

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

Unread post by kev1n »

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")');
   }
}
Timo
Posts: 217
Joined: Thu Mar 15, 2018 9:26 pm
Has thanked: 1 time

Re: Download to CSV

Unread post by Timo »

all right, thank you.
kev1n
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

Unread post by kev1n »

Your're welcome!
mariri
Posts: 45
Joined: Mon Sep 02, 2019 11:54 am

Re: Download to CSV

Unread post by mariri »

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 ? :shock:

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