0

I'm getting results from a SOAP call, but cannot display the values in a select list on my custom module. This is a part of my code:

foreach($sxml2->NewDataSet->DepCity as $item) {
    $fromName = $item->Name;     
    echo '<li>' . $fromName . '</li>'; //It displays correctly  


    $form['from'] = array(
        '#type' => 'select',
        '#title' => t('From'),
        '#options' => $fromName, 
    );      
}

$sxml2 is a simplexml load string result and it's display correctly. When I echo the result from the loop ($fromName), I'm getting all the strings properly, but If I try to put that variable in a custom form select, it doesn't display any data.

How can I get the results in a Drupal form select?

EDIT:

I've tried with this, but it still doesn't display anything in that select.

echo '<pre>';
print_r($sxml2); //The whole simpleXML object with arrays is displaying properly    
echo '</pre>';


$options = array();
foreach($sxml2->NewDataSet->DepCity as $item) {
  $options[$item->Name] = $item->Name;
  echo '<li>' . $item->Name . '</li>'; //It displays correctly  

}

$form['from'] = array(
  '#type' => 'select',
  '#title' => t('From'),
  '#options' => $options, //It doesn't display the data
); 

1 Answer 1

0

The #options property should be an array of key value pairs (the key is the <option>'s value attribute, the value is the content of that tag), so:

$options = array();
foreach($sxml2->NewDataSet->DepCity as $item) {
  $options[$item->Name] = $item->Name;
}

$form['from'] = array(
  '#type' => 'select',
  '#title' => t('From'),
  '#options' => $options,
);

That's just for demonstration, you may wish to use something other than a name for the option key - it's the sort of thing that would clash easily.

1
  • Hi, Clive. I've tried with your example, but it still doesn't display anything. I've tried as a test with $test = array(1,2,3); and set that array in options and it's displays the data, so I don't have any problem with the form. It just doesn't display the data from SOAP inside that form. I've edited my thread. Feb 1, 2016 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.