我需要将数据库中的数据显示为下拉列表。
这就是我的尝试:
Model.php
public function get_province()
{
$this->db->order_by('provincename');
$query = $this->db->get('tblprovince');
return $query->result_array();
}
Controller.php这样
public function __construct()
{
parent::__construct();
$this->load->model('placemodel','tblplace');
$this->load->model('placemodel', 'tblprovince', TRUE);
}
public function index()
{
$data['provinces'] = $this->tblprovince->get_province();
$data = array('title' => 'Places');
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('places/index',$data);
}
public function ajax_list()
{
$list = $this->tblplace->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $place) {
$no++;
$row = array();
$row[] = $no;
$row[] = $place->placename;
$row[] = $place->provincename;
$row[] = $place->placetypename;
$row[] = $place->tel;
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_person('."'".$place->placeid."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_person('."'".$place->placeid."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->tblplace->count_all(),
"recordsFiltered" => $this->tblplace->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
View.php
<div class="form-group">
<label class="control-label col-md-3">Province</label>
<div class="col-md-9">
<select name="province_id" class="form-control">
<option value="">Select Province</option>
<?php foreach ($provinces as $province): ?>
<option value="<?php echo $province['provinceid']; ?>"><?php echo $province['provincename']; ?></option>
<?php endforeach; ?>
</select>
<span class="help-block"></span>
</div>
</div>
但结果没有出现在省模式上。它没有出现在下拉列表中。
我做错了什么?
答案 0 :(得分:0)
因为在为provinces
指定值后,您的数据数组会被覆盖
将索引方法更改为
public function index()
{
$data = array('title' => 'Places');
$data['provinces'] = $this->tblprovince->get_province(); // place here
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('places/index',$data);
}
答案 1 :(得分:0)
我认为你需要在provinces变量之前分配数组。
它是在$data['provinces']
之后分配的
所以现在$data
只有这个值数组(&#39; title&#39; =&gt;&#39; Places&#39;);
public function index()
{
$data['provinces'] = $this->tblprovince->get_province();
$data = array('title' => 'Places');
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('places/index',$data);
}