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.
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Download to CSV

Unread post by ricklincs »

Hi Ke1vin

It comes up with access denied, but looks like it probably would work if access to procedure was granted.

Regards

Rick

I have tried running it on 2 different installs of nuBuilder on 2 different host servers and still the same problem.
Thanks for your input so far.
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 »

Please try again
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Download to CSV

Unread post by ricklincs »

Hi kev1n

That works in all my browsers. So something to do with my installation of nuBuilder as I have just copied and pasted your example code.
Thanks for your help

Rick
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 »

I dumped my form + procedure into an sql file. (Procedure renamed to BrowseDownloadToCSV2 so it won't conflict with yours).
You can import it into your existing DB and see if it works (see instructions here: https://github.com/smalos/nuBuilder4-Co ... ql_file.md)
You do not have the required permissions to view the files attached to this post.
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 »

ricklincs wrote:Hi kev1n

That works in all my browsers. So something to do with my installation of nuBuilder as I have just copied and pasted your example code.
Thanks for your help

Rick
I cannot think of anything that would change the behaviour.
Janusz
nuBuilder Team
Posts: 506
Joined: Fri Dec 28, 2018 1:41 pm
Location: Krakow, Poland
Has thanked: 8 times
Been thanked: 18 times

Re: Download to CSV

Unread post by Janusz »

removed - not to mix with main thread
Last edited by Janusz on Wed Aug 26, 2020 12:38 pm, edited 1 time in total.
If you like nuBuilder, please leave a review on SourceForge
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Download to CSV

Unread post by ricklincs »

Hi Kev1n

Thank you for the sql file and your input. I have discovered that the problem appears to be only with certain browse forms.

If I have SELECT * FROM stkhead in the sql of the Form everything works fine, but if I have SELECT * FROM stkhead WHERE ((status = '4') AND (companyname = 'MOTOR HOMES')) for example the browse form works, but the csv download just goes to a blank screen with no 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 »

You might have to encode the browse_sql. Could you give this a try?

Updated BrowseDownloadToCSV.php:
https://github.com/smalos/nuBuilder4-Co ... dToCSV.php

JavaScript Code:
(Also updated in the article: https://github.com/smalos/nuBuilder4-Co ... /README.md)

Code: Select all

function base64encode(str) {
	let encode = encodeURIComponent(str).replace(/%([a-f0-9]{2})/gi, (m, $1) => String.fromCharCode(parseInt($1, 16)))
	return btoa(encode)
}

function browseDownloadToCSV() {
	nuSetProperty('browse_sql', base64encode(JSON.stringify(nuCurrentProperties().browse_sql)));
	nuRunPHP('BrowseDownloadToCSV');
}

if(nuFormType() == 'browse'){
    nuAddActionButton('browseDownloadToCSV', 'Download to CSV', 'browseDownloadToCSV();');
}
ricklincs
Posts: 107
Joined: Mon Aug 01, 2011 5:39 pm
Has thanked: 33 times

Re: Download to CSV

Unread post by ricklincs »

Kev1n

Thank you for all your help and efforts. That has worked a treat!

Kind Regards

Rick
tpolimeni28
Posts: 25
Joined: Mon Apr 04, 2022 4:00 pm
Has thanked: 1 time

Re: Download to CSV

Unread post by tpolimeni28 »

Hello I am new here, I was adding in the download to CSV function. IN the process I used the code from GitHub_browse_download_to_CSV when I insert the code and add the custom code to the database. I click the download to CSV button which opens a new window and provides the following error. Can anyone provide some help with this. Last I have tried the code also posted in this forum from 2019 which just opens a blank window.

Error!: SQLSTATE[HY000] [2002] No such file or directory

Code Used under procedure

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 the Browse SQL from hash cookie
$sql  = json_decode(base64_decode("#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);
code used on form in JS area.

Code: Select all

function base64encode(str) {
	let encode = encodeURIComponent(str).replace(/%([a-f0-9]{2})/gi, (m, $1) => String.fromCharCode(parseInt($1, 16)))
	return btoa(encode)
}

function browseDownloadToCSV() {
	nuSetProperty('browse_sql', base64encode(JSON.stringify(nuCurrentProperties().browse_sql)));
	nuRunPHP('BrowseDownloadToCSV');
}


if(nuFormType() == 'browse'){
    nuAddActionButton('browseDownloadToCSV', 'Download to CSV', 'browseDownloadToCSV();');
}
Post Reply