Fatal error: Call to undefined method CI_Loader::result()

#1
[eluser]face1m[/eluser]
I am following the second video tutorial on creating a blog. All of the code has worked up to this point. But i can not find the cause of this error. See code below. thanks in advance for your help. I'm using php 5
rick


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?=$heading?&gt;</h1>

&lt;?php foreach ( $this->result() as $row ): ?&gt;

<h3>&lt;?=$row->title?&gt;</h3>
<p>&lt;?=$row->body?&gt;</p>
<hr />

&lt;?php endforeach; ?&gt;
------------------------------------------------------------------------

&lt;?php

class Blog extends Controller {

function __construct()
{
parent::Controller();
}



function index()
{

$data['title'] = 'My Blog Title';
$data['heading'] = 'My Blog Heading one';
$data['query'] = $this->db->get('enteries');


$this->load->view('blog_viewer', $data);
}

}
?&gt;

&lt;/body&gt;
&lt;/html&gt;

#2
[eluser]WebsiteDuck[/eluser]
In your controller, change:

Code:
function index()
  {
  
  $data['title'] = 'My Blog Title';
  $data['heading'] = 'My Blog Heading one';
  $query = $this->db->get('enteries'); //Possible typo here? enteries
  $data['results'] = $query->result_array();
  
  $this->load->view('blog_viewer', $data);
  }

In your view change:
Code:
&lt;?php foreach ( $results as $row ): ?&gt;

#3
[eluser]Jamie Rumbelow[/eluser]
Hello Rick,

Firstly, welcome to the CodeIgniter community!

You're trying to call result() on the CI master object - which is definitely not a good idea. result() is a method given to you by the DB result object, which is returned by $this->db->get() or $this->db->query(). In your controller, you assigned the result object to $data['query'], so in the view it's available as $query.

Knowing this, we just have to change the foreach loop in your view and it all should work:

Code:
&lt;?php foreach($query->result() as $row): ?&gt;

When in doubt, read the user manual - it's very comprehensive. Hope this has helped.

Jamie

#4
[eluser]face1m[/eluser]
Jamie websiteduck:

Thanks its working now. Many thanks
rick


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.