Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Created December 19, 2015 03:26
Show Gist options
  • Save khoand0000/425847f2c51d9dfa9c22 to your computer and use it in GitHub Desktop.
Save khoand0000/425847f2c51d9dfa9c22 to your computer and use it in GitHub Desktop.
foreach is not working in piklist

I want to generate 10 controls by piklist by foreach. But the code is not working

$values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($values as $value) {
  
  piklist('field', array(
      'type' => 'text',
      //'field' => '_field',
      'label' => 'field',
      'value' => $value,
  ));
}

Above code just show only control, value is last of $values. To achieve my purpose, there is only way, write 10 piklist() manually

@sbruner
Copy link

sbruner commented Dec 20, 2015

You're very close. Try this:

$values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach ($values as $value) {

  piklist('field', array(
      'type' => 'text',
      'field' => 'field_' . $value,
      'label' => 'field ' . $value
  ));
}

The 'value' parameter sets the default value for the field.

For a faster response try posting your questions on the Piklist Support Forum: piklist.com/support/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment