开发者

Convert Array suitable for CI dropdown

开发者 https://www.devze.com 2023-03-17 04:30 出处:网络
I\'m using a CodeIgniter method (db->result_array()) that returns me the following array: Array ( [0] => Array

I'm using a CodeIgniter method (db->result_array()) that returns me the following array:

 Array
 (
[0] => Array
    (
        [id] => 5
        [lang_name] => ASM
    )

[1] => Array
    (
        [id] => 16
        [lang_name] => Bash
    )

[2] => Array
    (
        [id] => 17
        [lang_name] => Batch
    )

[3] => Array
    (
        [id] => 3
        [lang_name] => C
    )

[4] => Array
    (
        [id] => 11
        [lang_name] => C#
    )

[5] => Array
    (
        [id] => 4
        [lang_name] => C++
    )
 )

And i need an array with this format, to pass it to the form_dropdown() CI function:

 $atrLang开发者_JS百科DropDown = array(
    '1' => 'CPP',
    '2' => 'PHP',
    '3' => 'ASM'
);

I've done something like this:

public function getAllLangsSelect() {
    $this->db->select('*')->from($this->tableName)->order_by($this->tableName.'.lang_name');
    $q = $this->db->get();
    $data = array();
    array_push($data, '- Select Language -');

    foreach ($q->result_array() as $row) {  
        array_push($data, $row['lang_name']);
    }

    return $data;

}

But it musts conserve the original ID stored in the DataBase, so it doesn't work as it should.

Any hint or help will be welcome. It's the first time when i do this kind of operations.


Try this:

public function getAllLangsSelect()
{
    $query = $this->db
        ->order_by('lang_name')
        ->get($this->tableName);

    $options[''] = '- Select Language -';

    foreach ($query->result() as $row)
    {  
        $options[$row->id] = $row->lang_name;
    }

    return $options;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号