Page 1 of 1

Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 3:54 pm
by Giu
Hi all,

I was playing with nuBuilder several years ago, but didn't had time to dive too much into it, and here we are. I'm working on a little web application for an association, and I'm searching several questions without success. For example.

- How to make SQL CRUD from PHP? There are specific functions for that? When an user enters on a form, I want to check if that user has a record on a table, and if not create it.
- I want to call another form/browse with a parameter from a button.

As you see, basic questions but I don't know if there are some place with basic tutorials functionality.
I see there are PHP and JS places to code, but I don't know the frameworks calls to do what I want.

Regards.

EDITED: To be more specific about SQL CRUD operations. Maybe I'm doing something wrong. I have a master detail. But each user only will have one master, and several details for that master. I can show a browse with records only for my user (one record BTW), and this do the job, because I don't like at all the GRID/FORM behaviour of subform objects, then what I'm looking for is.
From Master Edit form to call the detail form in creation mode, passing the PK to use it as FK, or from master browse, add a button to call the detail edit form in creation mode, and there add the specific logic to get the ID of the record of that user. In the middle, if the user master record doesn't exists, to create it.

Re: Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 4:24 pm
by kev1n
There is a function called nuRunPHPHidden() to run a PHP Procedure.

In the Procedure, you can check do checks and run sql.

Example PHP code:

Code: Select all

function hasUserRecordInTable($userId) {

    $selectSQL = "SELECT * FROM my_table WHERE user_id = ? ";
    $stmt = nuRunQuery($selectSQL , [$userId]);

    return db_num_rows($stmt) === 1;

}

function createRecordInTable() {

    $insertSql = "
		INSERT INTO `my_table` 
		  (`column1`, `column2`, `column3`)
		VALUES
		  (:column1, :column2, :column2)
	";

    $params = ["column1" => 'value 1 here', "column1" => 'value 2 here', "column1" => 'value 3 here'];

    nuRunQuery($insertSql, $params, true);

}

if (!hasUserRecordInTable('#USER_ID#')) {
    createRecordInTable()
}

- I want to call another form/browse with a parameter from a button
Add a filter in the Run button or use nuSetProperty() to set a hash cookie and use that in the Browse SQL.

Just ask if you need more information.

Re: Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 4:45 pm
by Giu
Oh interesting, will take a look, thanks a lot.

It's possible to integrate with other libraries? If I want to call some another PHP lib or service is possible?

Re: Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 4:59 pm
by kev1n
Yes, it should be possible.

Re: Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 5:09 pm
by Giu
Ok, thanks a lot, I think I have enough to start diving a little more. Thanks again.

Re: Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 9:54 pm
by Giu
I followed your suggestion and worked.

I have this code on a procedure. It's a modified version where always return the ID I need.

Code: Select all

function hasUserRecordInTable($userId) {

    $selectSQL = "SELECT * FROM fichas_prm WHERE usuario = ? ";
    $stmt = nuRunQuery($selectSQL , [$userId]);
    
    if (db_num_rows($stmt) === 1){
        $r  = db_fetch_object($stmt);    
        return $r->fichas_prm_id;
    } else {
        return createRecordInTable($userId);
    }

}

function createRecordInTable($userId) {

    $insertSql = "
		INSERT INTO `fichas_prm` 
		  (`usuario`, `prm`, `fichas_prm_id`)
		VALUES
		  (:usuario, :prm, :fichas_prm_id)
	";

    $newID = nuID();
    $params = ["usuario" => $userId, "prm" => 0, "fichas_prm_id" => $newID];

    nuRunQuery($insertSql, $params, true);
    return $newID;

}

nuJavaScriptCallback("
    nuSetProperty('PRMID', '".hasUserRecordInTable('#USER_ID#')."');
");
And if I call the procedure on an button added with nuAddActionButton() and so on, I can get the value with nuGetProperty, but now I'm trying to use it BEFORE EDIT to be launch directly and have the value instanty once the form is called for add new record, but I don't get the value returned. On the other side, once I get it don't have too much clear where exactly to use it, on a hidden input associated to the FK field? It's a subform BTW, but can change it. It's how I'm doing it the way to go? Maybe my proc should only create if not exists, and search for it on Before save maybe, and set the FK value there? but how?

Regards.

Re: Basic Knowledge and FAQs?

Posted: Wed May 15, 2024 10:16 pm
by kev1n
In Before Edit, use nuAddJavaScript() instead of nuJavaScriptCallback()