Page 1 of 2
Button that Generates a File
Posted: Tue Jun 18, 2019 6:49 pm
by Alohajoe5
I have an SQL statement that--essentially creates a tmp table, selects the contents, and writes the contents to a file on the SQL server. What would be the best way to attach this function to a button in nubuilder? I tried adding an object as a Select & Display and dumping my SQL in the SQL boxes, but that didn't seem to work. I don't really have any need for anything to be displayed when this button is clicked outside possibly a confirmation that the file has been generated but even this isn't necessary. The statement works when I run it as a query but I just don't know how to attach it in nubuilder. Any thoughts?
Re: Button that Generates a File
Posted: Sun Jun 23, 2019 12:50 am
by admin
Alohajoe5,
nuRunPHPHidden() can be run off a Button's click.
Does this help...
https://wiki.nubuilder.cloud/ ... nPHPHidden
Steven
Re: Button that Generates a File
Posted: Tue Jun 25, 2019 9:38 pm
by Alohajoe5
Steven,
I'm thinking that PHP invoking a stored procedure is my best bet. I don't need it to echo back anything so I think my php in nuBuilder should just be:
Code: Select all
function getChannels_Pipe_Delimited
$q=$cn->exec("CALL Procedure_Files_Channels()");
I got that from googling around--I'm not as versed in PHP. Does that look right? I take it from my other php entries in nuBuilder the <?php definition is unnecessary.
Re: Button that Generates a File
Posted: Wed Jul 24, 2019 5:18 pm
by Alohajoe5
So I have generated a view which will be called by a PHP function. The PHP will need to be attached to a button click as mentioned previously. Do I do this by adding an object? If so what "Type" of button do I select? Run? Also, I can't seem to find where I would attach PHP. I saw options for JavaScript under "Custom Code" but I haven't seen any area for PHP.
Second question is related to what would go into a PHP section. I recall inserting PHP in other parts of nuBuilder and I recall not including the full php definitions, but I don't know if I will need to declare the parameters for the mysqli connection to the database. My full php looks like this:
Code: Select all
<?php
$servername = 'myserver';
$username = 'user';
$password = 'password';
$dbname = 'mydb';
//Create a connection
$con = new mysqli($servername, $username, $password, $dbname, 3306);
$sql = "SELECT * FROM My_View";
$result = $con->query($sql);
$rowcount = 0;
$filecount = 1;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()){
// build logic for splitting file.
$rowcount = $rowcount + 1;
if (($rowcount % 10000) == 0) {
$filecount = $filecount + 1;
}
// join row elements into a string
$rowstr = join(",",$row);
//String Replace commas with Pipes
$rowstr2 = str_replace (',', '|', $rowstr);
//Count number of pipes
$fields = substr_count($rowstr2, '|');
//Calculate the number of empty fields & insert pipes
$emptyfields = str_repeat('|', (26-$fields));
//Concatenate data with empty fields
$rowstr2 .= $emptyfields;
$rowstr2 .= PHP_EOL;
$filename = 'signals2_' . $filecount . '.dat';
//Write data to file
file_put_contents ($filename, $rowstr2, FILE_APPEND);
}//end while
}
else {
echo "0 results";
}
?>
What can/should I cut out of this when I'm executing PHP in nuBuilder? Where should the PHP be placed?
Re: Button that Generates a File
Posted: Wed Jul 24, 2019 11:30 pm
by admin
Alohajoe5,
Create an Input Object of
Input Type Button and create an onclick event in Custom Code.
input.JPG
Steven
Re: Button that Generates a File
Posted: Thu Jul 25, 2019 3:39 pm
by Alohajoe5
Steven,
Thanks for the reply! When I go to custom code, I can type in "onclick" in the event, but, I am only given the option to enter javascript--not php. Am I missing something?
Re: Button that Generates a File
Posted: Thu Jul 25, 2019 10:48 pm
by Alohajoe5
So I looked at this question on StackOverflow:
https://stackoverflow.com/questions/128 ... -on-a-link
And I've generated the following JavaScript that I input into the Custom Code Section:
Code: Select all
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function Outfiles_JS() {
$.get("Outfiles.php");
return false;
}
</script>
I have a php file named "Outfiles" in /var/www/html. I tried once with just "Outfiles.php" and once with the name of our localserver/Outfiles.php and neither seemed to work. Is this not the correct way to use JavaScript to envoke a php file? In the Custom Code I just typed "onclick" and dumped the JavaScript in the Javascript section.
EDIT: I don't know how this would work since PHP runs server side and JavaScript runs client side?
Re: Button that Generates a File
Posted: Fri Jul 26, 2019 12:03 am
by admin
Alohajoe5,
This Javascript function calls the PHP Procedure you want by its code,
INV
Capture.JPG
https://wiki.nubuilder.cloud/ ... nPHPHidden
Steven
Re: Button that Generates a File
Posted: Mon Jul 29, 2019 3:59 pm
by Alohajoe5
Steven,
Thanks for the help. I've put the custom code in as an "onclick" under Event. In the Javascript section I've tried:
Code: Select all
nuRunPHPHidden('INV', Signals_Outfiles.php);
nuRunPHPHidden('INV', 'Signals_Outfiles');
nuRunPHPHidden('INV', 'Signals_Outfiles.php');
My php file: Signals_Outfiles.php lives in /var/www/html. Am I not using the nuRunPHPHidden correctly or do I have to place my php file somewhere else for nuBuilder to see it?
Re: Button that Generates a File
Posted: Mon Jul 29, 2019 4:17 pm
by Alohajoe5
Ok, so I figured out from one of the videos that I likely need to insert my php not as a file but as a procedure "run type" hidden.
I've edited out the portion of the declaration of the file as PHP, edited the connection part to a NuRunQuery and removed the original "else Echo" portion of the script--but this doesn't seem to be generating the files. Below is the code I placed in the Procedure PHP:
Code: Select all
$con = nuRunQyery($sql);
$sql = "SELECT * FROM My_View";
$result = $con->query($sql);
$rowcount = 0;
$filecount = 1;
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()){
// build logic for splitting file.
$rowcount = $rowcount + 1;
if (($rowcount % 10000) == 0) {
$filecount = $filecount + 1;
}
// join row elements into a string
$rowstr = join(",",$row);
//String Replace commas with Pipes
$rowstr2 = str_replace (',', '|', $rowstr);
//Count number of pipes
$fields = substr_count($rowstr2, '|');
//Calculate the number of empty fields & insert pipes
$emptyfields = str_repeat('|', (26-$fields));
//Concatenate data with empty fields
$rowstr2 .= $emptyfields;
$rowstr2 .= PHP_EOL;
$filename = 'signals2_' . $filecount . '.dat';
//Write data to file
file_put_contents ($filename, $rowstr2, FILE_APPEND);
}//end while
}