1

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

0

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

What if he has more than one row?
OP is using $this->db->limit(1); , so OP is expecting single row data in return :)
Didn't noticed that! Good answer!
its work, thanks sensei, u have a good understanding, noticing thats limit :D @JigarShah
Glad to help you @emo , don't forget to upvote answers if you found useful
0

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

Comments

0

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" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.