Hello,

I have a problem that I am having a difficult time finding solutions for: How do I loop in order to fill repetitious webform fields that are incremented by number. I have 10 sets of the same fields that are identified by the number at the end of the field name.

Here is some non verified code that follows a few tutorials I have found:

$i=1;
 foreach ($building->residential as $propertyInfo) {
 $node->building_address['und'][0]['value'] = $propertyInfo->address;
 $node->building_city['und'][0]['value'] = $propertyInfo->city;
 $node->building_state['und'][0]['value'] =  $propertyInfo->state;
 $node->building_zip['und'][0]['value'] = $propertyInfo->zip;
i++;
}

My webform fields are building_address 1 through 10 as well as city state and zip, 40 in total. I'd like to append a number on the end of the field name that. How do I populate these fields by adding a variable in the name?

Im wanting something like
$node->building_address{$i}['und'][0]['value'] = $propertyInfo->address;
$node->building_city{$i}['und'][0]['value'] = $propertyInfo->city;

Ultimately, I'd like values assigned to
$node->building_address1
$node->building_address2
$node->building_address3
etc...

Thanks for your time and help.

Comments

thetailwind’s picture

I can make a variable named:

$building_address = 'building_address' .$i . ' [\'und\'][0][\'value\']'; 
$node-> $building_address = $propertyInfo->address;

and then use that in place, but it looks really ugly. Is there a more streamlined approach?