array to select (Drop Down)

How to populate a drop-down list with dynamic year data in ChronoForms.

Overview

The issue occurs when an array is not formatted correctly for use with the Dynamic Data feature of a CF drop-down element.
Reformat the PHP array to use the required structure with 'value' and 'text' keys, then configure the drop-down element's Dynamic Data settings to reference this array.

Answered
le le5 23 Jun, 2014
I have following Code in "Custom Code" Event defined as "Controller":

<?php
date_default_timezone_set("Europe/Zurich");
$startJahr = date('Y') ;
$startJahr = $startJahr - 4;
$endJahr = $startJahr - 96;

$form->data['jahre'] = array();

for ($i = $startJahr; $i >= $endJahr ; $i--) {
   $form->data['jahre'][$i] = $i;
}
?>


How can I applay this array to a "Drop Down" element?

Any advise?

Thanks for any help
Gr GreyHead 23 Jun, 2014
Answer
Hi le5,

If you change the format of your data a little then you can use the Dynamic Data tab on a drop-down element. This requires the data to be in this format:
array(
  [0] => array( 'value' => 'some_value', 'text' => 'some_text' ),
  [1] => array( 'value' => 'some_other_value', 'text' => 'some_other_text' ),
  . . .
);

If you alter your code to be like this:
<?php
date_default_timezone_set("Europe/Zurich");
$jahr = date('Y') ;

$form->data['jahre'] = array();
for ($i = $jahr - 4; $i >= $jahr - 96; $i--) {
   $form->data['jahre'][] = array( 'value' => $i, 'text' => $i);
}
?>
Then use 'jahre', 'value' and 'text' in the Dynamic Data settings.

Note: you don't have to use 'text' and 'value' as the labels and they don't have to be the same.

Bob
le le5 23 Jun, 2014
Thanks worked nicely!
This topic is locked and no more replies can be posted.