Zend_Form - Select Box Question

3 messages Options
Embed this post
Permalink
Tina Matter

Zend_Form - Select Box Question

Reply Threaded More More options
Print post
Permalink
Hello -

I am selecting data from a database to populate a select box.   I am using the fetchPairs database option, so my first element is the value and the second element is the display value.

I also want a default option that has a null value at the top of my select box.
So here is my code:

$db_readPeopleAndGroups = Zend_Db::factory($config->db_readPeopleAndGroups);
$db_readPeopleAndGroups->getConnection();
$queryResults = $db_readPeopleAndGroups->fetchPairs("SELECT id, last_name FROM person ORDER BY last_name");
$results = $queryResults;

// array_unshift adds elements to the beginning of an array
array_unshift($results, array(''=>'Choose Faculty'));

// Faculty Plan View Select Box
$tpFacultyView = new Zend_Form_Element_Select('tpFacultyView');
$tpFacultyView->setLabel($config->tpFacultyView)
                         ->setMultiOptions($results)


However, when I display my form, Zend_Form puts <optgroup> tags around the "Choose Faculty" element that I added to the beginning of my array.

Here is a subset of what my source looks like:

<select name="tpFacultyView" id="tpFacultyView">
   <optgroup label="0">
   <option value="" label="Choose Faculty">Choose Faculty</option>
   </optgroup>
   <option value="1" label="Doe, John">Doe, John</option>
   <option value="2" label="Johnson, Julie">Johnson, Julie</option>
   <option value="3" label="Smith, Joe">Smith, Joe</option>
</select>

Is there any way to get rid of these <optgroup> tags?  For some reason, when clicking on my select box, I see the '0' above the "Choose Faculty" option (which is also indented from the other options).

If there's a better way to add the "Choose Faculty" option so that I don't get the <optgroup> tags, that would be great too.

Any help is appreciated.

Thanks bunches.
Tina Matter
Bart McLeod

Re: Zend_Form - Select Box Question

Reply Threaded More More options
Print post
Permalink
I use this and it works:
        $shop = new WebShop();
        $options = array();
        $options[0] = '----- Select category -----';
        $options = array_merge($options, $shop->getCategories());
        $select->setMultiOptions($options);
So in your case, you will probably have to use:

array_unshift($results, array(0=>'Choose Faculty'));

instead of

array_unshift($results, array(''=>'Choose Faculty'));

Regards,

Bart McLeod



Tina Matter schreef:

> Hello -
>
> I am selecting data from a database to populate a select box.   I am using
> the fetchPairs database option, so my first element is the value and the
> second element is the display value.
>
> I also want a default option that has a null value at the top of my select
> box.
> So here is my code:
>
> $db_readPeopleAndGroups = Zend_Db::factory($config->db_readPeopleAndGroups);
> $db_readPeopleAndGroups->getConnection();
> $queryResults = $db_readPeopleAndGroups->fetchPairs("SELECT id, last_name
> FROM person ORDER BY last_name");
> $results = $queryResults;
>
> // array_unshift adds elements to the beginning of an array
> array_unshift($results, array(''=>'Choose Faculty'));
>
> // Faculty Plan View Select Box
> $tpFacultyView = new Zend_Form_Element_Select('tpFacultyView');
> $tpFacultyView->setLabel($config->tpFacultyView)
>                          ->setMultiOptions($results)
>
>
> However, when I display my form, Zend_Form puts <optgroup> tags around the
> "Choose Faculty" element that I added to the beginning of my array.
>
> Here is a subset of what my source looks like:
>
> <select name="tpFacultyView" id="tpFacultyView">
>    <optgroup label="0">
>    <option value="" label="Choose Faculty">Choose Faculty</option>
>    </optgroup>
>    <option value="1" label="Doe, John">Doe, John</option>
>    <option value="2" label="Johnson, Julie">Johnson, Julie</option>
>    <option value="3" label="Smith, Joe">Smith, Joe</option>
> </select>
>
> Is there any way to get rid of these <optgroup> tags?  For some reason, when
> clicking on my select box, I see the '0' above the "Choose Faculty" option
> (which is also indented from the other options).
>
> If there's a better way to add the "Choose Faculty" option so that I don't
> get the <optgroup> tags, that would be great too.
>
> Any help is appreciated.
>
> Thanks bunches.
> Tina Matter
>  
Tina Matter

Re: Zend_Form - Select Box Question

Reply Threaded More More options
Print post
Permalink
Thanks for the reply, Bart.

I ended up doing this and it worked:

$db_readPeopleAndGroups = Zend_Db::factory($config->db_readPeopleAndGroups);
$db_readPeopleAndGroups->getConnection();
$queryResults = $db_readPeopleAndGroups->fetchPairs("SELECT id, last_name FROM person ORDER BY last_name");
$results = $queryResults;

$tpFacultyView->setLabel($config->tpFacultyView)
                     ->addMultiOption('', 'Choose Faculty')
                     ->addMultiOptions($results)


Although, I keep your idea in mind in case I ever need it for anything else.   I didn't even think about the array_merge option.  :-)

Have a great day.
Tina


Bart McLeod wrote:
I use this and it works:
        $shop = new WebShop();
        $options = array();
        $options[0] = '----- Select category -----';
        $options = array_merge($options, $shop->getCategories());
        $select->setMultiOptions($options);
So in your case, you will probably have to use:

array_unshift($results, array(0=>'Choose Faculty'));

instead of

array_unshift($results, array(''=>'Choose Faculty'));

Regards,

Bart McLeod



Tina Matter schreef:
> Hello -
>
> I am selecting data from a database to populate a select box.   I am using
> the fetchPairs database option, so my first element is the value and the
> second element is the display value.
>
> I also want a default option that has a null value at the top of my select
> box.
> So here is my code:
>
> $db_readPeopleAndGroups = Zend_Db::factory($config->db_readPeopleAndGroups);
> $db_readPeopleAndGroups->getConnection();
> $queryResults = $db_readPeopleAndGroups->fetchPairs("SELECT id, last_name
> FROM person ORDER BY last_name");
> $results = $queryResults;
>
> // array_unshift adds elements to the beginning of an array
> array_unshift($results, array(''=>'Choose Faculty'));
>
> // Faculty Plan View Select Box
> $tpFacultyView = new Zend_Form_Element_Select('tpFacultyView');
> $tpFacultyView->setLabel($config->tpFacultyView)
>                          ->setMultiOptions($results)
>
>
> However, when I display my form, Zend_Form puts <optgroup> tags around the
> "Choose Faculty" element that I added to the beginning of my array.
>
> Here is a subset of what my source looks like:
>
> <select name="tpFacultyView" id="tpFacultyView">
>    <optgroup label="0">
>    <option value="" label="Choose Faculty">Choose Faculty</option>
>    </optgroup>
>    <option value="1" label="Doe, John">Doe, John</option>
>    <option value="2" label="Johnson, Julie">Johnson, Julie</option>
>    <option value="3" label="Smith, Joe">Smith, Joe</option>
> </select>
>
> Is there any way to get rid of these <optgroup> tags?  For some reason, when
> clicking on my select box, I see the '0' above the "Choose Faculty" option
> (which is also indented from the other options).
>
> If there's a better way to add the "Choose Faculty" option so that I don't
> get the <optgroup> tags, that would be great too.
>
> Any help is appreciated.
>
> Thanks bunches.
> Tina Matter
>