2015-09-17 112 views
1

我試圖從數據庫中獲取的一些價值觀和我得到這個錯誤在讀取值從數據庫笨

一個PHP錯誤遇到
嚴重性:注意
消息:未定義指數:命名
文件名:觀點/ pro_view.php
行號:12

我的控制器:

$this->load->model('profile_model'); 

$data['profile_data'] = $this->profile_model->get_records(); 

$this->load->view('pro_view', $data); 

型號:

function get_records() 
{ 
    $r = $this->uri->segment(3); 
    $query = $this->db->get_where('profile', array('id' => $r)); 
    return $query->result(); 
} 

查看:

<input type="text" name="name" value="<?php echo $profile_data["name"] ?>" /> 

如果我var_dump(($profile_data);我得到陣列中的所有內容。

我讀了其他類似的問題和他們的答案,沒有運氣。

+0

默認情況下,CI返回爲對象,並且您需要爲每個要打印的值$ profile_data [0] - >名稱 –

+0

這樣回顯,這正是我所需要的。 – Dray

回答

3

如果您需要獲取單行然後用

$query = $this->db->get_where('profile', array('id' => $r)); 
$ret = $query->row(); 
return $ret->name; 

控制器

$data['name'] = $this->profile_model->get_records(); 

$this->load->view('pro_view', $data); 

查看

<input type="text" name="name" value="<?php echo $name;?>" /> 

而對於需要使用foreach loop

您的本次代碼

result()

此方法返回查詢結果作爲對象的陣列,或在失敗的 空數組。通常你會在foreach循環

+0

我有20行類型的值,你知道像一個正常的配置文件。姓名,電話等 – Dray

+0

您需要使用foreach循環,因爲我在回答中發帖 – Saty

+0

我收到錯誤。視圖中的$ profile_data [0] - > name'對我很有用。 – Dray

2

控制器

$this->load->model('profile_model'); 

    $data['profile_data'] = $this->profile_model->get_records(); 

    $this->load->view('pro_view', $data); 

型號

$r = $this->uri->segment(3); 
$this->db->get('*'); 
$this->db->from('profile'); 
$this->db->where('id',$r); 
$q = $this->db->get(); 
if($q->num_rows() > 0){ 
$row = $q->row(); 
return $row->name; 
} 
return false; 

視圖

<input type="text" name="name" value="<?php echo $name;?>" /> 
使用