2017-09-14 5 views
1

おはよう先生。コードイグナイターフレームワークの初心者。コードイグナイタで特定のデータを呼び出す方法について質問したいと思います。私のコードは私のデータベースから画像の名前を呼び出すことを目的としたものです。コードイグナイタを使用してデータベース内の特定のデータを呼び出す方法

この私のモデルは

function profile_picture(){  
$username = $this->session->userdata('name'); 
$this->db->select('pict_name'); 
$this->db->from('picture'); 
$this->db->where('username="$username"'); 
$this->db->limit(1); 
$query = $this->db->get(); 
return $query; 
} 

これは私のコントローラ

public function v_profile(){ 
    $data['ppicture'] = $this->m_profile->profile_picture()->result(); 
    $data['profile'] = "profile"; 
    $this->load->view('header',$data); 
    $this->load->view('profile',$data); 
    $this->load->view('footer',$data); 
} 

これが私の見解です

<img src="<?php echo base_url()."assets/img/".$ppicture; ?>" class="img-rounded" > 

上のコードはエラーを示しています:配列から文字列への変換。どうすれば修正できますか?おかげ

答えて

2

その与えてエラーを$data['profile']='profile';をassingingので$query = $this->db->get(); Beforこの

function profile_picture(){  
$username = $this->session->userdata('name'); 
$this->db->select('pict_name'); 
$this->db->from('picture'); 
$this->db->where('username="$username"'); 
$this->db->limit(1); 
$query = $this->db->get(); 
return $query->result_array(); 
} 

はそう$data['ppicture']をオブジェクトを返して試してみてください、result()は、その配列を提供

を変更する必要があります。result()row()

<img src="<?php echo base_url()."assets/img/".$ppicture->pict_name; ?>" class="img-rounded" > 

から

$data['ppicture'] = $this->m_profile->profile_picture()->row(); 

$ppicture;あなたは詳細についてdocumentationを参照することができ

+0

彼は1行以上のものを持っている場合はどう? – chad

+1

OPは '$ this-> db-> limit(1);'を使用しているので、OPは1行のデータが返されると期待しています:) –

+0

それに気付かなかった!いい答えだ! – chad

0

オブジェクトは、その後、あなたがそうあなたは、配列にアクセスしている

0

これは

function profile_picture(){  
$username = $this->session->userdata('name'); 
$this->db->select('pict_name'); 
$this->db->from('picture'); 
$this->db->where('username="$username"'); 
$this->db->limit(1); 
$query = $this->db->get(); 
return $query->result_array(); 
} 
にあなたのモーダルを変更 役立つかもしれません

あなたのビューページで

<img src="<?php echo base_url(); ?>assets/img/<?php echo $ppicture[0]['table filed name for image'];?>" class="img-rounded" > 
関連する問題