Reputation: 616
good evening sensei. im newbie in code igniter framework. i want to ask about how to call specific data with code igniter. my code below intended to call picture's name from my database.
this my Model
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;
}
this is my controller
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);
}
this is my view
<img src="<?php echo base_url()."assets/img/".$ppicture; ?>" class="img-rounded" >
the codes above shows error: array to string conversion. how can i fix it? thanks
Upvotes: 1
Views: 98
Reputation: 1521
This might help you
Change your Modal to
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();
}
In your view page
<img src="<?php echo base_url(); ?>assets/img/<?php echo $ppicture[0]['table filed name for image'];?>" class="img-rounded" >
Upvotes: 0
Reputation: 6223
You are accessing array, result()
provides array so ,
need to change result()
to row()
$data['ppicture'] = $this->m_profile->profile_picture()->row();
and $ppicture;
to $ppicture->pict_name;
<img src="<?php echo base_url()."assets/img/".$ppicture->pict_name; ?>" class="img-rounded" >
You can refer to documentation for further information
Upvotes: 2
Reputation: 831
Try This
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();
}
Befor $query = $this->db->get();
is returning object so $data['ppicture']
because an object and after that you assinging $data['profile']='profile';
so its giving error
Upvotes: 0