Hello I got the this result
A PHP Error was encountered
Severity: Error
Message: Cannot use object of type CI_DB_mysqli_result as array
Filename: controllers/Threads.php
Line Number: 48
Backtrace:
This is my codes in Controller
// category
public function threadsview($slug) {
// get threads category
$data['item'] = $this->threads_model->getThreadsCategory($slug);
$data['title'] = $data['item']['catname'];
$data['desc'] = '';
$this->load->view('themes/default/header', $data);
$this->load->view('threads/threadsview', $data);
$this->load->view('themes/default/footer');
}
And the codes in my Model
//get threads categories by slug
public function getThreadsCategory($slug) {
$this->db->order_by('id','asc');
$this->db->where('slug',$slug);
$query = $this->db->get('threads_cat');
return $query;
}
The error is from this code
$data['title'] = $data['item']['catname'];
I hope someone can help me regarding on this..
Thank you for your response I`ve made it and its working now
public function threadsview($slug) {
// get threads category
$data['item'] = $this->threads_model->getThreadsCategory($slug);
if($data['item']->num_rows()<=0){
$data['title'] = 'Opps!, 404 page not found';
$this->load->view('themes/default/header', $data);
$this->load->view('errors/html/error_404');
$this->load->view('themes/default/footer');
}else
foreach ($data['item']->result() as $res) {
$data['title'] = $res->catname;
$data['desc'] = $res->description;
$this->load->view('themes/default/header', $data);
$this->load->view('threads/threadsview', $data);
$this->load->view('themes/default/footer');
}
}
$data['item']
is not an array, probably because you didn't actually fetch the results into an array.
Inside your function, fetch first the result of the query into an array.
public function getThreadsCategory($slug) {
$this->db->order_by('id','asc');
$this->db->where('slug',$slug);
$query = $this->db->get('threads_cat');
$result = $query->result_array();
return $result;
}
What you need to do is get a result or row, and then check if there was anything in it. So when you do your query:
$query = $this->db->get('threads_cat');
Before you return anything you should do this:
if( $query->num_rows() > 0 )
{
return $query->result_array();
}
return FALSE;
Or if you are just expecting a single row:
if( $query->num_rows() == 1 )
{
return $query->row_array();
}
return FALSE;
Be sure to check if $data['item'] == FALSE
before trying to use it:
if( $data['item'] !== FALSE && is_array( $data['item'] ) )
{
// $data['item'] is an array and can be used as an array
}
Make sure your returning an array or an object in your modal
$data['item'] = $this->threads_model->getThreadsCategory($slug);
in your model if your using
$this->db->from($table); $query = $this->db->get(); return $query->row();
// this is returning the first row as object & not array
In controller
$data['title'] = $data['item']->catname;
for array you could use something like this
$query->first_row('array');
//this is returning the first row as Array
In controller
$data['title'] = $data['item']['catname'];
//if your still having a problem you could print your data in controller just comment your load->view() and add this line
print_r("<pre>"); print_r($data['item']); print_r("</pre>");