简体   繁体   English

Codeigniter:result_array()到form_multiselect()

[英]Codeigniter: result_array() to form_multiselect()

I am pulling a queried result_array() and placing that array as the fields to select from in a form_multiselect(). 我拉一个查询的result_array()并将该数组作为要在form_multiselect()中选择的字段。 What I can't seem to figure out is why the multi-select shows the array indexes followed by each queried result. 我似乎无法弄清楚为什么多重选择会在每个查询结果后面显示数组索引。 Is this a problem with my array or are there form_multiselect options I am missing for the array indexes to not be shown? 这是我的数组有问题吗?还是缺少我无法显示的数组索引的form_multiselect选项?

And my code below: 而我的代码如下:

public function get_tags() {
    $this->db->select('tag_name');
    $this->db->distinct();
    $this->db->from('offers_tags');

    $query = $this->db->get();

    $tags = $query->result_array();

    return $tags;  
}

My controller: 我的控制器:

$this->data['tags']=$this->offer_model->get_tags();

My view: 我的观点:

<div class="control-group">
            <?= form_label('Tags: ', 'tag_targets', $label_attr);?>

            <div class="controls">
                <?= form_multiselect('tag_targets[]',$tags,'','id="geo-select"');?>
            </div>
</div>

Maybe You have the order of the parameters wrong. 也许您的参数顺序错误。

The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html 第一个参数将包含字段名称,第二个参数将包含选项的关联数组,第三个参数将包含您希望选择的一个或多个值http://ellislab.com/codeigniter/user-guide /helpers/form_helper.html

You could also var_dump the array to check the values if you're not sure 如果不确定,也可以使用var_dump数组检查值

I do not think you are specifying the options correctly. 我认为您没有正确指定选项。 http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

you should be specifying the options like this: 您应该指定这样的选项:

$tags = array(
    'tag1Value'  => 'tag1Name',
    'tag2Value'  => 'tag2Name',
    'tag3Value'  => 'tag3Name',
    'tag4Value'  => 'tag4Name'
);

However what you are returning from your get_tags function is: 但是,您从get_tags函数返回的内容是:

array(
    0 => array('tag_name'=> 'Tag1'),
    1 => array('tag_name'=> 'Tag2'),
    2 => array('tag_name'=> 'Tag3')
)

Each result is a separate array contained with a wrapping array. 每个结果是一个包装数组包含的单独数组。

You can get the results into the format needed for form_multiselect by looping through the items and creating a new array. 您可以通过遍历项目并创建一个新数组,将结果转换为form_multiselect所需的格式。 In your get_tags function you can do the following. 在您的get_tags函数中,您可以执行以下操作。

$tags = array();
foreach ($query->result_array() as $row)
{
   $tags[$row['tag_name']] = $row['tag_name'];
}
return $tags;

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

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