How to use dynamic field names?

Hi,

i want to change a lot of fields in a form with data from a database.

i just want to look up the the id of the field in the database and then change the value of the field like this:

$kto = "Kto".{ac_names[$i][2]};
{$kto} = {ac_names[$i][3]}

the field i want to change the value ist “Kto1”, “Kto2”, …

but it doesnt work. the applications just doesnt start anymore…

is there some kind of error log?

and how am i supposed to do such things?

thanks a lot,

Mati

Re: How to use dynamic field names?


$kto = "Kto".{ac_names[$i][2]};
{$kto} = {ac_names[$i][3]}

Try to remove the ‘$’ from the field macro … so it should be {kto}, not {$kto}.

{field} = $this->field

to simplify:
{kto} = “Kto”.{ac_names[$i][2]};

Regards,
Scott

Re: How to use dynamic field names?

Hi Scott,

thanks for your answer.

but how do i access that particular field denn?

i have “kto1” in my variable and want to put some data in the field “kto1”.

{kto} = “Kto”.{ac_names[$i][2]};

can i access it via {{kto}}?

what would you do?

thanks in advance,

mati

Re: How to use dynamic field names?

I am not sure I understood the mixup.

Several things. I have found that in SC that if you create a var with the same name as the field, it will cause interference.

$kto1 generates: $this->kto1
{kto1} generates: $this->kto1

you may want to create another name for your var, or underscore it: $_kto1

so:
$_kto1 = “Kto”.{ac_names[$i][2]};
{kto} = $_kto1;

this will generate $this->kto1 = $this->_kto1;

You should be able to simply access your field using {fieldname}. since you creating a var with the same name, it is overriding the field value so you cannot access.

If this is not the case, then please try again, perhaps using different values for the fieldname vs the variable vs the values… It is a bit confusing.

Regards,
Scott.

Re: How to use dynamic field names?

Hi Scott,

thanks for your answer.

im sorry i wrote my problem so confusing, im gonna try to explain it more clearly.

i have a formular with a couple of fields with the names kto1, kto2, kto3, kto3, kto5, kto6

in my database are values for each of these fields labeled with 1,2,3,4,5,6.

now i want to write these values in the fields. i just want to put the field (to write it in) dynamically together with the values from the database.

normally i would access the fields like this:

{kto1} = …;
{kto2} = …;

but i dont want to type that all. i want to do it from the values of the database.

it should work something like that:

for ($i = 1; $i<=6; i++) {
{kto$i} = {ac_names[$i][1]};
}

but {kto$i} is not working, {kto{i}} either.

do you know what i am after?

thanks a lot,

mati

Re: How to use dynamic field names?

I do not believe you will be able to use a SC macro to do this. You may have to go low level to the field array for the form.

Form onLoad:
// build field name
$field_name = ‘tick’ . ‘er’;
// display field
echo ‘>’.$_SESSION[‘sc_session’][‘1’][‘form_edit_current’][‘dados_select’][ $field_name ]."<br>".PHP_EOL;
// display form field for reference
// print_r($_SESSION[‘sc_session’][‘1’][‘form_edit_current’][‘dados_select’]);

// change value in form
$_SESSION[‘sc_session’][‘1’][‘form_edit_current’][‘dados_select’][ $field_name ] = ‘123456’;
// display new value for field
echo ‘>>’.$_SESSION[‘sc_session’][‘1’][‘form_edit_current’][‘dados_select’][ $field_name ]."<br>".PHP_EOL;

-My field name was ticker that I built using $field_name.
-Make sure you place spaces around field_name (or your var) to Sc will not try to make a global var
-When you load the form, you can use View/Data in Session from the menu to see your path under sc_session

There might be an easier approach … like assigning the session array to a local var, then play with it.

Regards,
Scott.