Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Procedure with parameters Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Procedure with parameters

Unread post by yvesf »

Hi,

I would like to create a procedure with parameters. It seems that inside procedure you can create functions with parameters.
What is the syntax ? How do you call a function in a procedure from the JavaScript ?
Many thx,

Yves
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Procedure with parameters

Unread post by kev1n »

Are you referring to a nuBuilder PHP procedure?
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Procedure with parameters

Unread post by yvesf »

yes nuBuilder PHP procedure
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Procedure with parameters

Unread post by kev1n »

I'm not sure if I fully understand what you mean. Could you describe a specific use case?

To pass parameters to a nuBuilder Procedure, you can use nuRunPHPHiddenWithParams.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Procedure with parameters

Unread post by yvesf »

Could you please show me an example of procedure with parameters ? I don't see how to put parameters when creating a nuBuilder PHP procedure
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Procedure with parameters

Unread post by kev1n »

A nuBuilder Procedure does not support direct parameters. However, you can still pass "parameters" using JavaScript, such as with nuRunPHPHiddenWithParams(). Search the forum for examples.
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Procedure with parameters

Unread post by yvesf »

To clarify my request I have 2 procedures with only one difference : normally you would create a function with one parameter.
Here are the 2 procedures .
Procedure 1

Code: Select all

$activityid1='#primary_Key#';

$query="select act_order from activite where activite_id='".$activityid1."';"; // on recupère ordre l'activité qu'on veut descendre d'un cran.
$order_ori=nuRunquery($query);
$r  = db_fetch_object($order_ori);
$order=$r->act_order;
$order_new=$order+1; //nouvel ordre de l'activité récupérée
$order_new_str=strval($order_new);;
$update = "Update activite set act_order=".$order." where act_order =".$order_new.";";// l'activité ayant l'ordre en dessous remonte vers le haut
$result = nuRunQuery($update);
$update2 = "Update activite set act_order=".$order_new." where activite_id='".$activityid1."';"; //affectation du nouvel ordre à l'activité sélectionnée
$result2 = nuRunQuery($update2);
$js = " nuGetBreadcrumb();";
nuJavaScriptCallback($js);
And procedure 2

Code: Select all

$activityid1='#primary_Key#';

$query="select act_order from activite where activite_id='".$activityid1."';"; // on recupère ordre l'activité qu'on veut descendre d'un cran.
$order_ori=nuRunquery($query);
$r  = db_fetch_object($order_ori);
$order=$r->act_order;
$order_new=$order-1; //nouvel ordre de l'activité récupérée
$order_new_str=strval($order_new);;
$update = "Update activite set act_order=".$order." where act_order =".$order_new.";";// l'activité ayant l'ordre en dessous remonte vers le haut
$result = nuRunQuery($update);
$update2 = "Update activite set act_order=".$order_new." where activite_id='".$activityid1."';"; //affectation du nouvel ordre à l'activité sélectionnée
$result2 = nuRunQuery($update2);
$js = " nuGetBreadcrumb();";
nuJavaScriptCallback($js);
The only difference between those 2 procedures is the line $order_new which is equal to $order+1 in procedure 1 and $order-1 in procedure 2. It is the only difference.
How do you manage this situation ?

Thx,

Yves
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: Procedure with parameters

Unread post by kev1n »

You can set a hash cookie using nuSetProperty() or nuRunPHPHiddenWithParams() with JavaScript and retrieve it with nuGetProperty() in PHP.
Then pass the Hash Cookie values to the php procedure updateActivityOrder()

In JavaScript, set the sort order Hash Cookie, e.g.

Code: Select all

nuSetProperty('sort_oder','up'); // or 'down'
nuRunPHPHidden(...) // call your procedure
Updated, consolidated PHP Procedure (untested):

Code: Select all

// Retrieve Hash Cookies

$activityId = nuGetProperty('primary_Key'); // Primary Key of the selected browse row
$sortOrder = nuGetProperty('sort_oder');	// Sort direction (up, down)

// Validate input

if (!$activityId || !in_array($sortOrder, ['up', 'down'])) {
    $js = " nuMessage('Invalid procedure parameters'); ";
} else {
    updateActivityOrder($activityId, $sortOrder);
    // Refresh the UI
    $js = "nuGetBreadcrumb();";
}

nuJavaScriptCallback($js);
	
function updateActivityOrder($activityId, $direction) {
    // Determine the order adjustment
    $orderChange = ($direction === 'up') ? -1 : 1;

    // Get the current order of the activity
    $query = "SELECT act_order FROM activite WHERE activite_id = ?";
    $order_ori = nuRunQuery($query, [$activityId]);
    $r = db_fetch_object($order_ori);

    if (db_num_rows($r) !== 1) {
        return; // If no record found, exit function
    }

    $order = $r->act_order;
    $order_new = $order + $orderChange;

    // Ensure the new order is valid
    if ($order_new < 1) {
        return; // Prevents setting a negative or zero order
    }

    // Swap the order values with safer queries
    $update = "UPDATE activite SET act_order = ? WHERE act_order = ?";
    nuRunQuery($update, [$order, $order_new]);

    $update2 = "UPDATE activite SET act_order = ? WHERE activite_id = ?";
    nuRunQuery($update2, [$order_new, $activityId]);
}

Post Reply