简体   繁体   English

动态将数据库表值添加到下拉列表

[英]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. 您没有提供太多有关其实际含义的信息,但现在我会假设您很难将数据库值添加到Select元素中。 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. 这是使用Zend Framework 2的ServiceManager完成的。

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 ;) Blog本身应该足以使您开始编写适当的SO-Question;)

What part of this is it you need help with? 您需要帮助的哪一部分? The HTML? HTML? The loop in PHP? PHP中的循环? HTML: 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: PHP,用于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 不要为最困难的事情而努力`在Controller中,添加

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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