简体   繁体   中英

Dynamically Add Database Table Values to Drop-down List

I have created custom plunging to get list of values from the database.

Now I want create a Drop-Down list using this array

This is my Select List

$this->add(array(
    'name' => 'group_name',
    'type' => 'select',
    'attributes' => array(
        'id'=>'group_name',
        'class'=>'large',
    ),
    'options' => array(
        'label' => 'Select List',
        'value_options' => array(
            '1' => 'php',
            '2' => 'java'
        ),
    ),
));

You do not give all too much information about what it actually is, that you have trouble with, but for now I will assume that you have trouble to get Database Values into your Select elements. For this, please see detailed information on my Blog:

The basic you need to understand is simple Depedency Injection. You will need to properly inject the Data-Sources (or the Data itself) into your Form. This is done using the ServiceManager of Zend Framework 2.

Since there are many different pathes one can choose (and even my Blog doesn't cover them all), i won't go into detail of any specific one until you request so. The Blog itself should be enough to get you started to be able to write a proper SO-Question ;)

What part of this is it you need help with? The HTML? The loop in PHP? HTML:

<form action="" method="get">
<select name="group_name">
  <option value="1">php</option>
  <option value="2">java</option>
</select>
</form>

The PHP, for the group_name:

echo $array['name'];

For the looping through the select values:

foreach($array['options']['value_options'] AS $key=>$option){
    echo '<option value="'.$key.'">'.$option.'</option>';
}

Something like this...???

Don't go for the hardest thing `In Controller, add

$urFormObject->get('selectOptionName')->setValueOptions($listArray);
Example: 

$formName->get('group_name')->setValueOptions($listArray);`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM