falak hamid
falak hamid

Reputation: 21

show retrieve data in drop down list in codeigniter

Controller code

<?php




class Booking_Controller extends CI_Controller{



        public function __construct(){


            parent::__construct();
            $this->load->model('Books_model');

        }


    public function view()
    {

        $data['result']=$this->Books_model->get_restaurants();

        $this->load->helper(array('form','url'));
        $this->load->view('restaurants/booking',$data);


    }


}

And its model code

<?php


class Books_model extends CI_Model{



    public function __construct(){

        $this->load->database();


    }

    public function get_restaurants()
    {
        $sql = "SELECT id, names FROM restaurants ";
        $query = $this->db->query( $sql );
        return $query->result();

    }

}

Plz guide me what code i written in view file that i get name list in dropdown form....

Upvotes: 0

Views: 2156

Answers (2)

shankar kumar
shankar kumar

Reputation: 648

write following code to show dropdown in views.

<select name="">
<?php foreach($result as $row){?>
 <option value="<?php echo $row->id; ?>"><?php echo $row->names;?></option>
 <? }?>
</select>

$data['result'] is converted to $result variable in view and hence you can use it as above mentioned.

Upvotes: 0

giordanolima
giordanolima

Reputation: 1218

In your controller:

$result = $this->Books_model->get_restaurants();
$data['select'] = Array();
foreach($result as $r){
    $data['select'][$r->id] = $r->names;
}
$this->load->helper(array('form','url'));
$this->load->view('restaurants/booking',$data);

In your view:

<?php echo form_dropdown('restaurant', $select);  ?>

Upvotes: 3

Related Questions