display data from database to dropdown CodeIgniter

后端 未结 6 1459
星月不相逢
星月不相逢 2020-12-03 08:54

I\'m having difficulty with display data from the db to dropdown.

This is what I have tried:

Model.php

        public funct         


        
6条回答
  •  情歌与酒
    2020-12-03 09:44

    Codeigniter already has specialized functions that minimize the amount of html that you have to dump in your code:

    Model

    public function description_pulldown(){
        $this->db->from('location');
        $query = $this->db->get();
        foreach($query->result() as $row ){
            //this sets the key to equal the value so that
            //the pulldown array lists the same for each
            $array[$row->description] = $row->description;
        }
        return $array;
    }
    

    Controller

    public function index(){
        $data['description_list'] = $this->delivery_model->description_pulldown();
        //load all of your view data
        $this->load->view('delivery_view', $data);
    }
    

    View

    echo form_label("Description");
    echo form_dropdown('description', $description_list, set_value('description'), $description_list);
    

    If you need to have the view pull up the previous data in the dropdown list, you can do a foreach loop to obtain the previous value of the dropdown from the database ie... $description = $item->description; and in the view, change the 'set_value('description')' to simply '$description.'

提交回复
热议问题