Question regarding custom global variable

Is it possible to generate custom global variables from the database?

for example: I have “conf” table in the database, there are “name” and “val” columns
example:

table col: name val

sitename My Site name
company_url www.company.com

I select everything from the table, and then in foreach to make global variables

so that in the end I get [sitename] and [company_url] …

is there any way to do this ?

Thanks in advance for any reply
Kind regards

Easy: Ensure the code is run when the Menu is loaded or the first application that is executed
$sql=“SELECT type,name FROM configurations where id = 1”;
sc_select(my_data, $sql);

$type = $my_data->fields[‘type’];
$name = $my_data->fields[‘name’];
sc_set_global($type);
sc_set_global($name);
now you can use [type] and [name] as global variables

1 Like

I don’t think you understood what I wanted, I tried all the possibilities and I didn’t succeed.
make global variables from data, see image, name and val
name to be the name of the variable, and the val to be the value of the variable
i want to get this from foreach
[smtp_server] = mail.mail.de
[smtp_user] = email@email.com
[smtp_pass] = mypass

here is an example

sc_lookup(records, “SELECT * FROM conf”);
$rows = {records};
if(!(bool)$rows){
         return false;
    } else {
foreach($rows as $row){
      $newVar = $row[2];
      $$newVar = $row[3];
  }
}

[smtp_email] = $smtp_email;
[smtp_pass] = $smtp_pass;
[smtp_port] = $smtp_port;
[smtp_server] = $smtp_server;
[smtp_tcp] = $smtp_tcp;

pay attention to the double dollar sign

this works, but how to generate global variables directly inside the foreach?

I almost do something the same but don’t use a loop but use the array search. Good examples are: PHP: array_search - Manual

$sql= “select name,val from configuration_db”;
sc_lookup(config, $sql);
$variable = {config}[array_search(‘search_item’,array_column({config},0))][1];

eg:
$smtp_server = {config}[array_search(‘smtp_server’,array_column({config},0))][1];
sc_set_global($smtp_server);

I hope this helps

you still don’t understand what I want, I’ll try to explain it better like this:

sc_lookup(records, “SELECT * FROM conf”);
$rows = {records};
if(!(bool)$rows){
         return false;
    } else {
foreach($rows as $row){
      $newVar = $row[2];
      $$newVar = $row[3];
  }
}

this is an example that works, but in this example they are not global variables
if I type echo after this code, I get the following

echo $smtp_server;
Result:   mail.your-server.de

echo $sms_sys_zum;
Result:   0

$smtp_server $sms_sys_zum and all other variables from the table are generated within foreach.

this is what I want, only to be global variables.

this is the part where the variable is generated $$newVar = $row[3];

I tried [$newVar] = $row[3]; but I didn’t succeed

I hope you understand now :slightly_smiling_face:

Sorry for not understanding the problem. I still dont. Perhaps somebody else will

1 Like

Hi @Kripton ,

Just s suggestion:

sc_lookup(records, “SELECT smtp_server, smtp_user, smtp_pass FROM conf”);
if(!isset({records[0][0]})) { // Not found
    return false;
} else {
    [n_smtp_server] = {records[0][0]};
    [n_smtp_user] = {records[0][1]};
    [n_smtp_pass] = {records[0][2]};
}

Specifying the fields will be used are important in order to does not get unnecessarily data from SELECT.
And remember that global variables or local variables must be different than the field names (that’s why I put a n_ before the global variable name).

Hi @Kleyber_Derick ,

if you have read well what I have written above, then you will understand that your answer is not what I am looking for.

If so many people don’t understand your question, perhaps it is not the answer, but the question?

4 Likes

ok, let’s try this, watch the video

Youtube

did you try sc_set_global($newVar);

Problem is that he is using $$ (double) which is a reference variable, and as far I know you cannot assign another variable to a reference variable.

it’s not a problem that I use $$, that’s how it should be, it’s a problem that I can’t make a global variable out of it …

then assign the $$ to a regular variable and make that one a global?

1 Like

Well can you assign it to a normal variable?

I can’t assign it to a regular variable because the variable got its name based on the data from the database.

sc_lookup(records, “SELECT * FROM conf”);
$rows = {records};
if(!(bool)$rows){
         return false;
    } else {
foreach($rows as $row){
      $newVar = $row[2];
      $$newVar = $row[3];

//this doesn't work 
sc_set_global($$newVar);


  }
}

Try use:

$GLOBALS[$newvar] = $value;

variables must always be defined pre compilation, i guess in javascript it is possible what you want but it is not a good practice anyway.

for this reason we have arrays. you better of making an associative array , the key - is your variable name, the value is the value. i am not sure SC allows keeping arrays in global variables,