Page 3 of 4

Re: Download to CSV

Posted: Tue Aug 25, 2020 5:16 pm
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.

Re: Download to CSV

Posted: Tue Aug 25, 2020 5:24 pm
by kev1n
Please try again

Re: Download to CSV

Posted: Tue Aug 25, 2020 5:30 pm
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

Re: Download to CSV

Posted: Tue Aug 25, 2020 5:45 pm
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)

Re: Download to CSV

Posted: Wed Aug 26, 2020 8:24 am
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.

Re: Download to CSV

Posted: Wed Aug 26, 2020 9:33 am
by Janusz
removed - not to mix with main thread

Re: Download to CSV

Posted: Wed Aug 26, 2020 10:32 am
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.

Re: Download to CSV

Posted: Wed Aug 26, 2020 11:24 am
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();');
}

Re: Download to CSV

Posted: Wed Aug 26, 2020 11:59 am
by ricklincs
Kev1n

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

Kind Regards

Rick

Re: Download to CSV

Posted: Mon Apr 04, 2022 7:05 pm
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();');
}