Variables as field names


I have a form with several validation conditions which would be easy to handle by working with arrays.

For this, I would need to reference field values, using variables… is there any way of doing this?

$arrayFieldNames = array(“fieldName1”,“fieldName2”,“fieldName3”);
foreach ($arrayFieldNames as $fieldName) {
if ( {$fieldName} == somevalue ) {
// do something to check for the conditions I need

the list of fields might be 20 or 30 fields long… that’s why I would like to use an array to work with them, instead of individual "If"s

I suppose that fieldnames are coming from a database table. So use a query to get those columns (fieldnames)


will give you all your fieldnames of a form based on a table

That might only work when using fields from an already populated form, AND when the form hasn’t been modified… but think about two different and common cases:

  • validating a new empty form… there’s no information on the database yet.
  • validating a form filled with data from a table, but which has already been modified by the user

On a new generated form and on a form that has been modified by the user the fields on the form still remain the same since they are linked to a table in the database.

My solution is a way to get the fieldnames (=form fields).

But if you use custom fields on your form the solution will not work. You will have to make your array yourself.

Thanks for your interest, but either I’m not understanding your logic, or I was unable to explain what I need in the first post.

If you check my example, I don’t just want to get the “field name”… i NEED the field name to get it’s current value, and with that, act accordingly to verify and validate the form.

I believe you suggest something like this:


This gives me the field names…but as you might see in my example, I already know the names… I need to dynamically use those names to theck the current field’s value

I am currently doing this verification, with more than 25 if’s and "switch"s… but with a code just as my example, I can do the same in just a few lines…
What I’m trying to do is learn a way to optimize my code, since that ammount of conditional checks should be really unnecessary.

The following would be a crude and somewhat useless code, but should illustrate my example… just think that instead of 5 fields, I have 30 for which I need to count value occurences which will validate the form.

Example (assume variables are initialized):
$arrayFieldNames = array(“fieldName1”,“fieldName2”,“fieldName3”,‘fieldName4’, ‘fieldName5’);
foreach ($arrayFieldNames as $fieldName) {
switch ( {$fieldName}) {
case "Y’ : $yesAnwers+=1;break;
case "N’ : $noAnswers+=1;break;
case “” : $emptyAnswers+=1;break;
// and here I would check the different combiantions of valid yes/no/empty answers

Unfortunately this is something I have searched for quite some time. But afaik there is no way of doing this, at least not a simple way.

Hi mkubota,
here is a sniplet, which might help you.

It gets the names and values of all fields in the current form.
Hint: Refers to all fields on the form not only fields bound to the database.

$my_fields = $this->nmgp_dados_form;
foreach($my_fields AS $myfield_name=>$myfield_value)
		echo "<br>" . $myfield_name . " - " . $myfield_value;

If you want to refer to specific fields (for example companyname), you can use:

$my_fields = $this->nmgp_dados_form;
echo $my_fields['companyname'];

Have a great day!

Gunter Eibl

Thank you Gunter…

It does work on forms.
I had tried to find the correct $this property, but was unable to do it.

In fact, I did my tests in control apps, and I had found my fields and values in what seems to be the “Db” property… but I have been unsuccessful using it… i’m not very good with objects…

try maybe with this :slight_smile:

$val_sel = "SELECT
AND column_name LIKE ‘part_of_your_table_name%’ " ;

$cnt = count(array_filter({lvs}));
echo $cnt;
$col_name = {lvs[0][0]};

$col_name = 0;
if($cnt > 0)
for($x = 0;$x < $cnt;$x++)
$col_names = {lvs[$x][0]};
$val_name_tbl = “select val_of_your_table_name,$col_names from your_table_name where $col_names > 0”.’
echo $val_name_tbl;

Hi nsch2308
Your code, also selects fields from the database, and in my case, I used an example similar to the first part of your answer just to illustrate what I believed a previous user (nonkelmike) was trying to explain.

I’m trying to dynamically “read” field values in form (or a control app, which might not have a database schema supporting it)

Gunter’s solution was exactly what I needed for forms, I just have to adapt it to control apps now.

Thanks for your interest anyway