Page 1 of 2

Use added user field(s) as variables for forms sql statement

Posted: Sun Jul 21, 2019 11:28 pm
by nub74
Hi to all,
I am new to nuBuilder and although I have searched in many topics
I have not found a simple answer to my problem. I added a new field (Department) to the users table and its form and a new field (Level) to the access table and its form.
Now I need to use the values ​​of these fields as variables for the sql statements of some forms by loading them on hash variables or in any other way
that makes this goal possible. How can I get this result?
Thanks to all.

Re: Use added user field(s) as variables for forms sql state

Posted: Sun Jul 21, 2019 11:58 pm
by admin
nub74,

If you change system tables (zzzzsys*) you will not be able to do any updates to your system without losing the changes you have made.

You should create separate tables that relate to the zzzzsys_user and zzzzsys_access tables.


Steven

Re: Use added user field(s) as variables for forms sql state

Posted: Mon Jul 22, 2019 12:19 am
by nub74
Steven,
thanks for your quick response. I will surely do as you suggested.
Now going back to the my question: how can I use the values of these fields
as variables for the sql statements of my forms?
Thank you.

Re: Use added user field(s) as variables for forms sql state

Posted: Mon Jul 22, 2019 12:40 am
by admin

Re: Use added user field(s) as variables for forms sql state

Posted: Mon Jul 22, 2019 9:01 pm
by nub74
Hi Steven,
somehow I reached my goal using the #USER_ID# variable within subqueries.
Now I need to ask for another suggestion: I have to fill a field using the variable #USER_ID# as default value;
I tried to follow the following topic https://forums.nubuilder.cloud/viewtopic. ... lue#p17210
but if I replace the word 'hello' with the variable #USER_ID# I get nothing.
How should I proceed?
Thanks a lot.

Re: Use added user field(s) as variables for forms sql state

Posted: Mon Jul 22, 2019 10:15 pm
by Janusz
Hi,
The #USER_ID# you can use in PHP.
In JS you can use:

Code: Select all

var x=nuGetProperty('user_id');
$('#con_user').val(x).change();
if you want directly user name you can use:
(for the globeadmin there is empty field so thats why it is used: if (nuUser === null) { nuUser = "admin";})

Code: Select all

function getUser() {
   var nuUser = nuUserName();
   if (nuUser === null) { nuUser = "admin";}
   return nuUser;
}

$('#con_user').val(getUser()).change();
to see the available properties open the Console with F12 and type in:
nuCurrentProperties()

to get the value for given parameter use in JS:
nuGetProperty('user_id')
(you can type in as well in the console to display)
https://wiki.nubuilder.cloud/ ... Javascript

Re: Use added user field(s) as variables for forms sql state

Posted: Mon Jul 22, 2019 11:22 pm
by nub74
Hi Janusz,
thank yoy for your response.
The code:

var x=nuGetProperty('user_id');
$('#con_user').val(x).change();

works correctly but:

1) when I save, focus always come back to the updated field (con_user) and save button become red again
2) saved value is not those is saw in add form

how should I proceed?

Re: Use added user field(s) as variables for forms sql state

Posted: Mon Jul 22, 2019 11:45 pm
by Janusz
Hi,
Apply this just for new record as following:

Code: Select all

if(nuCurrentProperties().record_id == '-1')
{
var x=nuGetProperty('user_id');
$('#con_user').val(x).change();
}
but not sure for your second question.
please give more details

Re: Use added user field(s) as variables for forms sql state

Posted: Tue Jul 23, 2019 3:05 pm
by nub74
Hi Janusz,
now the code for the default values works correctly. Thank you.
I have to return to the matter of the fields used as variables for the slq statements (see my first post).
I thought I could solve with the subqueries but it is not so. I followed the post that Steven proposed to me
(https://forums.nubuilder.cloud/viewtopic.php?f=20&t=9953) but I have to set as property not a fixed value
but one read from a field of other table. How can I do this?
Thanks in advance.

Re: Use added user field(s) as variables for forms sql state

Posted: Tue Jul 23, 2019 4:25 pm
by Janusz
Hi,
I do not know any details of your code so can only show general example how to read a value from any table inside the php code.
You can easilly use any mysql query and get any data you want from your tables.
Maybe it will help somehow.

Code: Select all

$table="rejestr";       // table name
$f1="rej_frozen";       // field from table 
$f2="rej_month_closed"; // field from table 
$id=$table."_id";       //rekord ID - table index
$r=64;  // just any existing index for example
$t="SELECT ".$f1.",".$f2." FROM ".$table." WHERE ".$id."=".'"'.$r.'"';
$x=nuRunQuery($t);
$y = db_fetch_object($x);
$f1v = $y->$f1;
$f2v = $y->$f2;

nuDebug($f1v,$f2v);  // just see in the debug list