2014-03-06 43 views
0

我的表看起來像試圖獲取非對象的屬性。顯示特定值笨

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

$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 
) 
+0

使用行()

<?=$value->column;?> //try this or <?=echo $value->column; ?> 

我認爲他們只是當我把where子句放在同一個地方時呢? – Kino

+0

是的,它只用where子句獲得一行,但row()和result()之間的數據以不同的方式返回。查看我的編輯例子。 – Samutz

0

如果你的模型中使用return $query->result()使用<?php echo $value[0]->column1; ?>

如果使用模型使用<?php echo $value->column1; ?>

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); 

} 
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); 

} 

查看:

在使用的結果()

<?php foreach($value as $val) : ?> 
    <tr> 
    <td><?=$val->column;?></td> 
    </tr> 
<?php endforeach; ?> 
相關問題