principiorum
principiorum

Reputation: 616

how to call specific data in database using code igniter?

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

Answers (3)

Yadhu Babu
Yadhu Babu

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

Jigar Shah
Jigar Shah

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

Vijay Sharma
Vijay Sharma

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

Related Questions