0

I populate an select list using the following code.

$getCompanies = 'SELECT nid, title FROM node where type = "my_type"';
        $companiesResult = db_query($getCompanies);
        $companyArray = array();
        $companyArray['empty'] = "";
        while ($data = db_fetch_object($companiesResult)) {
            $companyArray[$data->nid] = $data->title;
        }
        $form['company'] = array(
            '#title' => t('Type'),
            '#type' => 'select',
            '#description' => t(''),
            '#options' => $companyArray,
        );
    }

This gives me an empty result first. I want to remove this empty result. I tried adding #required by it does not add a default selection. What can I do to remove an empty element?

2

The blank option was added by yourself:

$companyArray['empty'] = "";

If you remove that line you wont have a blank line, or alternatively your could change it to

$companyArray['empty'] = t("Please select");

To make the phrase Please select appear as default in the select option.

0

Try adding the property: '#required' => TRUE into $form['company'].

  • I did. It adds an * by the field but it does not remove the blank option. – Jeff May 5 '11 at 13:04
  • Can you dpm() the array $companyArray ? – Pol Dellaiera May 5 '11 at 13:19
  • And also, what's the use of that line $companyArray['empty'] = ""; ? – Pol Dellaiera May 5 '11 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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