我的表格似乎是
name column1 column2
p1 2 3
p2 4 6
我只希望在我的视图页面中显示
2
在我的控制器页面中:
public function table(){
$this->load->model('table_model');
$data['value']= $this->table_model->getData();
$this->load->view('table', $data);
}
在我的模型页面中:
public function getData(){
$this->db->select(*);
$this->db->from('table');
$this->db->where("name = 'p1'");
$query = $this->db->get();
return $query->result();
}
在我的“视图”页面中:我尝试了
<?php echo $value->column1; ?>
但是给了我一个错误
Message: Trying to get property of non-object
答案 0 :(得分:0)
$query->result()
返回行数组
如果您只想查找第一行,请使用return $query->row();
或$value[0]->column1
。
编辑例如:
$query->result()
返回:
Array (
[0] => Object (
name => p1
column1 => 2
column2 => 9
)
)
$query->row()
返回:
Object (
name => p1
column1 => 2
column2 => 9
)
答案 1 :(得分:0)
如果您的模型使用return $query->result()
使用<?php echo $value[0]->column1; ?>
如果您的模型使用return $query->row()
使用<?php echo $value->column1; ?>
答案 2 :(得分:0)
模型 -
public function getData(){
$this->db->select(*);
$this->db->from('table');
$this->db->where("name = 'p1'");
$query = $this->db->get();
$data = $query->result();
foreach($data as $row){
$value = $row->column1;
}
return $value;
}
视图 -
<?php echo $value; ?>
控制器 -
public function table(){
$this->load->model('table_model');
$data['value']= $this->table_model->getData();
$this->load->view('table', $data);
}
答案 3 :(得分:0)
试试此代码
型号:
public function getData($p1){
$this->db->where('name = p1'); // you can use this or this $this->db->where('name',$p1);
$query = $this->db->get('table');
return $query->result(); // if you use result() you will use in the view is foreach to display the data but if you use row() you can directly call the data i will give example
}
控制器:
public function table(){
$this->load->model('table_model');
$data['value']= $this->table_model->getData();
$this->load->view('table', $data);
}
查看:
使用row()
<?=$value->column;?> //try this or
<?=echo $value->column; ?>
使用result()
<?php foreach($value as $val) : ?>
<tr>
<td><?=$val->column;?></td>
</tr>
<?php endforeach; ?>