Reputation: 63
I have a link ending with index.php/welcome/item/1
I have my controller welcome.php with the function item
public function item($id)
{
$data = array();
$this->load->helper('url');
$data['title'] = "item";
$this->load->view('header', $data);
$data['item']=$this->port_model->get_item($id);
$this->load->view('item',$data);
$this->load->view('footer');
}
and my model with the get_item function
public function get_item($id)
{
$this->db->select('*');
$this->db->from('portfolio');
$this->db->where('id', $id);
$query = $this->db->get();
return $item = $query->result();
}
In my item view i want to echo info out of the database. Why doesn't it do that now
<h1>item</h1>
<?php echo $item->naam; ?>
Error i get on the page: A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/item.php
Line Number: 2
The page gets loaded normal, no problems. What am i doing wrong?
Upvotes: 0
Views: 647
Reputation:
You need to call the library database ( $this->load->database();
) and the model ($this->load->model('port_model');
) :
Result:
<code>public function item($id)
{
$data = array();
$this->load->helper('url');
$data['title'] = "item";
$this->load->view('header', $data);
$this->load->database();
$this->load->model('port_model');
$data['item']=$this->port_model->get_item($id);
$this->load->view('item',$data);
$this->load->view('footer');
}</code>
Upvotes: 0
Reputation: 431
Replace in your get_item
function this :
result()
This function returns the query result as an array of objects, or an empty array on failure.
return $item = $query->result();
Like this :
row()
This function returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.
return $item = $query->row();
Upvotes: 0
Reputation: 3253
Since you're expecting one single result, your get_item function should
return $query->row();
Upvotes: 1