我是CodeIgniter的新手,我创建了一个简单的应用程序,它将从数据库中获取数据,然后在<SELECT>
下拉列表中显示它。我正在尝试从数据库中的特定字段获取数据到我的视图。到目前为止,我已经尝试了下面的代码(不工作):
我的模型(datamodel.php),
function getbanklist() {
$banklist = array();
$this->db->select("id, bank");
$this->db->from('bank');
$query = $this->db->get();
if ($query->num_rows >= 1){
foreach($query->result_array() as $row){
$banklist[$row['id']]=$row['bank'];
}
return $banklist;
}
}
我的控制器(home.php),
function index(){
$data['bankdata'] = $this->datamodel->getbanklist();
$this->load->view('viewdata', $data);
}
我的观点(viewdata.php),
<tr>
<th>BANK</th>
<td>
<div class="containers">
<select name="bank">
<?php foreach($bankdata as $bank){
echo '<option value="'.$bank['id'].'">'.$bank['bank'].'</option>';
} ?>
</select>
</div>
</td>
</tr>
我的数据库结构(参见here),
id bank ------------ 0 Bank 1 1 Bank 2 2 Bank 3 3 Bank 4 4 Bank 5
答案 0 :(得分:3)
试试这个:
型号:
function getbanklist() {
$this->db->select("id,bank");
$this->db->from('bank');
$query = $this->db->get();
return $query;
}
在您看来:
<select name="bank">
<?php foreach($bankdata->result() as $bank){ ?>
<option value="<?php echo $bank->id ?>"><?php echo $bank->bank ?></option>
<?php } ?>
</select>
答案 1 :(得分:0)
如何将数据库值传递给codeingniter中的查看页面下拉列表。
这是我的HTML代码:
D:\demo\webrtcDemo\samples\test>node server.js
D:\demo\webrtcDemo\samples\test\server.js:20
key: keys.serviceKey,
^
TypeError: Cannot read property 'serviceKey' of undefined
at D:\demo\webrtcDemo\samples\test\server.js:20:14
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:278:24
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:168:24
at done (D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:78:24)
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:91:13
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:1044:20
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:1027:9
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:937:20
at D:\demo\webrtcDemo\samples\node_modules\pem\lib\pem.js:1092:20
at F (D:\demo\webrtcDemo\samples\node_modules\pem\node_modules\which\which.j
s:68:16)
这是我的控制器代码:
<div class="form-group">
<select name="department" id="department">
<?php foreach($bankdata as $key => $value) { ?>
<option value="<?php echo $value['dept_id']; ?>"><?php echo $value['managers_name']; ?></option>
<?php } ?>
</select>
</div>
这是我的型号代码:
public function department()
{
$this->load->model('insert_model');
$data['bankdata'] = $this->insert_model->getbanklist();
//$this->load->view('login_view', $data);
$this->load->view('login_view',$data);
}