Page 1 of 4

Download to CSV

Posted: Wed Oct 16, 2019 5:36 pm
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?

Re: Download to CSV

Posted: Wed Oct 16, 2019 10:48 pm
by admin
Timo,

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

Steven

Re: Download to CSV

Posted: Tue Oct 22, 2019 7:09 pm
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.

Re: Download to CSV

Posted: Tue Oct 29, 2019 5:27 am
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

Re: Download to CSV

Posted: Tue Oct 29, 2019 8:24 am
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)!

Re: Download to CSV

Posted: Wed Oct 30, 2019 11:26 pm
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?

Re: Download to CSV

Posted: Sat Nov 23, 2019 10:49 am
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")');
   }
}

Re: Download to CSV

Posted: Thu Dec 05, 2019 7:34 am
by Timo
all right, thank you.

Re: Download to CSV

Posted: Thu Dec 05, 2019 4:36 pm
by kev1n
Your're welcome!

Re: Download to CSV

Posted: Thu Dec 12, 2019 5:30 pm
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 !