控制器代码
<?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);
}
}
及其型号代码
<?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指导我在视图文件中写了哪些代码,我以下拉形式获得名单....
答案 0 :(得分:3)
在您的控制器中:
$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);
在您看来:
<?php echo form_dropdown('restaurant', $select); ?>
答案 1 :(得分:0)
编写以下代码以显示视图中的下拉列表。
<select name="">
<?php foreach($result as $row){?>
<option value="<?php echo $row->id; ?>"><?php echo $row->names;?></option>
<? }?>
</select>
$ data [&#39; result&#39;]在视图中转换为$ result变量,因此您可以按上述方式使用它。