我在CI中遇到此错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: loai
Filename: views/thembds.php
Line Number: 57
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/thembds.php
Line Number: 57
我的控制器:
public function loaibds()
{
$this->load->model('admin_model');
$data['loai'] = $this->admin_model->loai();
$this->load->view('thembds', $data);
}
我的模特:
public function loai()
{
$l = $this->db->query("SELECT * FROM loaibds");
if($l->num_rows() > 0)
return $l->result();
return false;
}
我的观点:
<select class="form-control" name="loai">
<?php foreach($loai as $l) { ?>
<option value="<?php echo $l->id; ?>"> <?php echo $l->loai; ?> </option>
<?php } ?>
</select>
我有什么错误?我想在视图页面上分配行和回显,例如
答案 0 :(得分:2)
$l = $this->db->get('loaibds')->result();
if($l->num_rows() > 0)
return $l;
}
答案 1 :(得分:0)
因为$l->result()
是资源,而不是结果。
将模型更改为以下内容:
public function loai()
{
$l = $this->db->query("SELECT * FROM loaibds");
$result = array();
if($l->num_rows() > 0)
foreach ($l->result() as $row)
$result = $row;
return $result;
}
现在应该可以了。
答案 2 :(得分:0)
更改模型
$l = $this->db->get('loaibds');
if($l->num_rows()>0)
{
return $l->result();
}
else{
return FALSE;
}