Button that Generates a File

Questions related to using nuBuilder Forte.

Button that Generates a File

Postby Alohajoe5 » Wed Jun 19, 2019 2:19 am

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?
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby admin » Sun Jun 23, 2019 8:20 am

Alohajoe5,

nuRunPHPHidden() can be run off a Button's click.

Does this help... https://wiki.nubuilder.net/nubuilderforte/index.php/Javascript#nuRunPHPHidden


Steven
admin
Site Admin
 
Posts: 3094
Joined: Mon Jun 15, 2009 9:53 am

Re: Button that Generates a File

Postby Alohajoe5 » Wed Jun 26, 2019 5:08 am

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.
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby Alohajoe5 » Thu Jul 25, 2019 12:48 am

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?
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby admin » Thu Jul 25, 2019 7:00 am

Alohajoe5,

Create an Input Object of Input Type Button and create an onclick event in Custom Code.

input.JPG
input.JPG (22.21 KiB) Viewed 113 times



Steven
admin
Site Admin
 
Posts: 3094
Joined: Mon Jun 15, 2009 9:53 am

Re: Button that Generates a File

Postby Alohajoe5 » Thu Jul 25, 2019 11:09 pm

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?
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby Alohajoe5 » Fri Jul 26, 2019 6:18 am

So I looked at this question on StackOverflow:

https://stackoverflow.com/questions/1280767/how-do-i-run-php-code-when-a-user-clicks-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?
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby admin » Fri Jul 26, 2019 7:33 am

Alohajoe5,

This Javascript function calls the PHP Procedure you want by its code, INV

Capture.JPG
Capture.JPG (30.21 KiB) Viewed 95 times


https://wiki.nubuilder.net/nubuilderforte/index.php/Javascript#nuRunPHPHidden


Steven
admin
Site Admin
 
Posts: 3094
Joined: Mon Jun 15, 2009 9:53 am

Re: Button that Generates a File

Postby Alohajoe5 » Mon Jul 29, 2019 11:29 pm

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?
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Re: Button that Generates a File

Postby Alohajoe5 » Mon Jul 29, 2019 11:47 pm

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
}
Alohajoe5
 
Posts: 55
Joined: Tue Apr 16, 2019 9:02 pm

Next

Return to General