Codeigniter form_helper getting database rows to be values in select menu

后端 未结 6 2117
借酒劲吻你
借酒劲吻你 2021-01-07 00:49

I am writing a form, which has a select menu in it, I want the values to pulled from the database, so I thought it would be something along these lines:

My view

相关标签:
6条回答
  • 2021-01-07 01:24

    form drop down with lot of options codeigniter form drop down menu with validation class also

    0 讨论(0)
  • 2021-01-07 01:27

    You need to pass an array to your dropdown, where the array key will be the value that is POSTed and the value will the text that is displayed.

    To achieve this, change your controller like so:

    function add_content() {
            $data = array();
            $this->is_logged_in();
            $this->load->model('category_model');
            $data['select_options'] = $this->category_model->get_all_online_select();
            $this->load->view('admin/content/add_content', $data);
    }
    

    and add this function to your model

    public function get_all_online_select() {
            $this->db->select('id, name'); //change this to the two main values you want to use
            $this->db->from('category');
            $this->db->where('category_online', 1);
            $query = $this->db->get();
            foreach($query->result_array() as $row){
                $data[$row['id']]=$row['name'];
            }
            return $data;
    }
    

    That should do the trick

    0 讨论(0)
  • 2021-01-07 01:32

    I have edited Phil Surgeon's array helper to work with a simple db query with only two fields (id & value). So the helper class now looks like this:

    <?php
    function array_to_select() {
        //get args
        $args = func_get_args();
        //get args key names
        $keys = array_keys($args[0][0]);
        //set return array
        $return = array();
        foreach ($args[0] as $itteration){
            //$itteration[$keys[0]] is field id value,  $itteration[$keys[1]] is field name value
            $return[$itteration[$keys[0]]] = $itteration[$keys[1]];
        }
        return $return;
    }
    

    And you can use it again in your controller.
    Hope it's usefull.

    0 讨论(0)
  • 2021-01-07 01:36

    I personally hate to make assumptions in my Models about how my data will be used as that is the job of the controller. If you add a MY_array_helper.php and paste this in:

    function array_to_select() {
    
    $args = func_get_args();
    
    $return = array();
    
    switch(count($args)):
    
        case 3:
            foreach ($args[0] as $itteration):
                if(is_object($itteration)) $itteration = (array) $itteration;
                $return[$itteration[$args[1]]] = $itteration[$args[2]];
            endforeach;
        break;
    
        case 2:
            foreach ($args[0] as $key => $itteration):
                if(is_object($itteration)) $itteration = (array) $itteration;
                $return[$key] = $itteration[$args[1]];
            endforeach;
        break;
    
        case 1:
            foreach ($args[0] as $itteration):
                $return[$itteration] = $itteration;
            endforeach;
        break;
    
        default:
            return FALSE;
        break;
    
    endswitch;
    
    return $return;
    

    }

    Then you can do something like this:

    function add_content() {
        $data = array();
        $this->is_logged_in();
        $this->load->model('category_model');
        $this->load->helper('array');
        $data['select_options'] = array_to_select($this->category_model->get_all_online(), 'id', 'title');
        $this->load->view('admin/content/add_content', $data);
    

    }

    That supports multi-dimensional arrays by passing in one or two keys, or single dimensional arrays by using the value as the value and the key.

    Eg: array_to_select(array('value1', 'value2')) gives array('value1'=>'value1', 'value2'=>'value2')

    0 讨论(0)
  • 2021-01-07 01:40

    You need to return an array of strings, result() is an array of objects.

    Maybe try this in your model:

    return $query->result_array();
    
    0 讨论(0)
  • 2021-01-07 01:42

    In your view, you can add foreach there instead of in Model.

    <?php
       echo form_open('admin/save_content');
       echo form_fieldset();
       foreach($select_options->result_array() as $row){
       $data[$row['id']]=$row['name'];
       echo form_dropdown('categories', $row);
       }
       echo form_submit('category_submit', 'Submit');
       echo form_fieldset_close();
       echo form_close();
    ?>
    

    Not tested.

    0 讨论(0)
提交回复
热议问题